当前位置:网站首页>Replace some escape characters with characters of the specified standard
Replace some escape characters with characters of the specified standard
2022-07-22 20:19:00 【R_ be still fresh fresh in one 's memories】
demand :
In each field A carriage return 、 A newline Use space characters (“ ”, Hexadecimal 0x20) Transference . English comma “,” Double quotes “"” Single quotation marks “’” And the vertical bar separator “|” according to RFC1738 Standards are used separately %2C、%22、%27、%7C Transference .
package com.rk.spark
import org.apache.commons.lang.StringEscapeUtils
/**
* @program: bd1809
* @class_name StringTest
* @author: rk
* @create: 2019-03-15 12:06
* @Description:
* In each field A carriage return 、 A newline Use space characters (“ ”, Hexadecimal 0x20) Transference . English comma “,” Double quotes “"”
* Single quotation marks “’” And the vertical bar separator “|” according to RFC1738 Standards are used separately %2C、%22、%27、%7C Transference .
*
*
* replace The parameter is char and CharSequence, That is, it can support character replacement , String substitution is also supported
* (CharSequence The meaning of string sequence , To put it bluntly, it's a string );
*
* replaceAll The parameter is regex, That is, replacement based on regular expression , such as : Can pass replaceAll("\\d", "*")
* Change all numeric characters of a string to asterisks ;
*
* The same thing : It's all replacement , That is, to replace a character or string in the source string with a specified character or string ;
*
* Difference :replaceAll regular expression , So the parameters are parsed ( Both parameters are ), Such as replaceAll("\\d", "*"),
* and replace Will not be ,replace("\\d","*") Just replace "\\d" String , It doesn't resolve to regular .
*
* There's another difference :“\” stay java Is an escape character , So we need two for one .
* for example System.out.println( "\\" ) ; Print only one "\". however “\” It's also the escape character in regular expressions , You need two for one .
* therefore :\\\\ By java convert to \\,\\ It's also converted into \, So with replaceAll Replace “\” by "\\",
* To use replaceAll("\\\\","\\\\\\\\"), and replace be replace("\\","\\\\").
*
**/
object StringTest {
def main(args: Array[String]): Unit = {
val str = "aa\raabbb\rccc\nsc"
println(str)
// Use StringEscapeUtils.escapeJava() You can display non escape strings
val out: String = StringEscapeUtils.escapeJava(str)
println(out)
println(out.replace("\\r", " "))
//replaceAll At the bottom is the regular expression used
println(out.replaceAll("\\\\r"," "))
println(getSpecialCharTransferField(str))
}
def getSpecialCharTransferField(field:String):String= {
var aa = field
var lastStr = ""
var lastStr01 = ""
var lastStr02 = ""
var lastStr03 = ""
var lastStr04 = ""
if(field.contains("\r")||field.contains("\n")){
val reg = "\r|\n".r
lastStr = field.replaceAll(reg.toString()," ")
aa = lastStr
}
if(aa.contains(",")||aa.contains("\"")||aa.contains("'")||aa.contains("|")){
lastStr01 = aa.replace(",","%2C")
lastStr02 = lastStr01.replace("\"","%22")
lastStr03 = lastStr02.replace("'","%27")
lastStr04 = lastStr03.replace("|","%7C")
aa = lastStr04
}
aa
}
}
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
For the above String Class replaceAll Source code , It can be seen that the bottom layer adopts regular expression .
By the way replace and replaceAll The difference between :
replace The parameter is char and CharSequence, That is, it can support character replacement , String substitution is also supported (CharSequence The meaning of string sequence , To put it bluntly, it's a string );
replaceAll The parameter is regex, That is, replacement based on regular expression , such as : Can pass replaceAll("\\d", "*") Change all numeric characters of a string to asterisks ;
The same thing : It's all replacement , That is, to replace a character or string in the source string with a specified character or string ;
Difference :replaceAll regular expression , So the parameters are parsed ( Both parameters are ), Such as replaceAll("\\d", "*"), and replace Will not be ,replace("\\d","*") Just replace "\\d" String , It doesn't resolve to regular .
There's another difference :“\” stay java Is an escape character , So we need two for one . for example System.out.println( "\\" ) ; Print only one "\". however “\” It's also the escape character in regular expressions , You need two for one . therefore :\\\\ By java convert to \\,\\ It's also converted into \, So with replaceAll Replace “\” by "\\", To use replaceAll("\\\\","\\\\\\\\"), and replace be replace("\\","\\\\").
If you want to replace only the first time , have access to replaceFirst(), This method is also based on the replacement of regular expressions , But with replaceAll() The difference is , Replace only the first occurrence of the string .
边栏推荐
- dns被劫持了怎么处理 5种方法教你处理
- Websites jump inexplicably. What is website hijacking from Baidu? DNS hijacking (domain name hijacking) what is DNS hijacking
- Typora download and simple use tutorial
- CPU亲和力
- 美国土安全局对特勤局删除的国会大厦骚乱期间电子通信记录展开调查
- 【C】从内存出发理解C语言变量作用域与生命周期
- Getting started with elastic search: installation and configuration of elastic search (I)
- 离线日志采集统计分析
- Kotlin learning three: set and lambda
- Error reported by Xcode compiling pod third-party library
猜你喜欢
Appstore upload screenshot size
求N!后面有多少个0
网络劫持是什么?网页被篡改劫持怎么修复(终级方案)网页劫持如何修复?
How to deal with DNS hijacking, DNS hijacking, and DNS hijacking solutions
类模板剖析
网站安全之域名被劫持、域名被劫持后该怎么办!!!
Getting started with elastic search: installation and configuration of elastic search (I)
离线日志采集统计分析
What should I do if the web page is hijacked? How to repair DNS hijacked? Introduction to web hijacking
How to repair DNS hijacking perfectly? How to solve DNS hijacking and how to repair it perfectly
随机推荐
PLSQL cannot be initialized
ARM及系列处理器的分类介绍
机器学习入门:线性回归-1
Comment le détournement de DNS peut - il être parfaitement réparé? Comment résoudre le problème du détournement de DNS
Easy operation commands of ES for getting started with elastic search (II)
[reading notes] MySQL architecture and storage engine
Flutter development (XXXI): flutter starts the white screen
Flutter 2 Advanced (I): practical skills of flutter
spark中使用Accumulator累加器使用和注意事项
RetinaNet:Focal Loss for Dense Object Detection
Liangning product course learning
网页被劫持了该怎么办?dns被劫持如何修复?网页劫持介绍
euler五十讲(一)
How to solve the blue screen problem in win8.1 system and how to solve the malicious hijacking of IE home page?
深入理解mmap函数
HDU 3351:Seinfeld
Kotlin learning 1: variables, functions, conditional statements and loop statements
Elastic Search 学习入门之核心概念(四)
How to clear localstorage data after closing the browser
使用CSDN-markdown编辑器入门