I, replace method
The function of this method is to replace all the specified characters in the string and then generate a new string. After the method call, the original string does not change. For example:
string s = "Abcat"; string s1 = S.replace (' A ', ' 1 ');
The purpose of this code is to replace all character A in string s with character 1, and the resulting new string S1 value is "1bc1t", while the contents of the string s do not change.
If you need to replace a specified string in a string with a different string, you can use the ReplaceAll method, for example:
string s = "Abatbac"; string S1 = S.replaceall ("ba", "12");
The purpose of this code is to replace all the string "AB" in the string s with "12", to generate a new string "a12t12c", and the contents of the string s will not change.
If you only need to replace the first occurrence of the specified string, you can use the Replacefirst method, for example:
string s = "Abatbac"; string S1 = S. Replacefirst ("Ba", "12");
The purpose of this code is to replace only the first occurrence of the string "AB" with the string "12", the value of the string S1 is "A12tbac", and the contents of the string s are not changed.
Replace and replaceall are commonly used substitution characters in Java, and their differences are:
1) Replace parameter is char and charsequence, that is, can support the substitution of characters, also support the substitution of strings (charsequence is the meaning of the string sequence, in other words, the string);
2) The ReplaceAll parameter is a regex, which is the substitution of a regular expression, for example, by ReplaceAll ("\\d", "*") to convert all numeric characters of a string to asterisks;
The same point is all replace, that is, a character or string in the source string is all replaced by the specified character or string, if you want to replace only the first occurrence, you can use Replacefirst (), this method is based on the substitution of regular expressions, but with ReplaceAll () The difference is that only the first occurrence of the string is replaced;
In addition, if the parameter data used by ReplaceAll () and Replacefirst () are not based on regular expressions, the effect of replacing the string with replace () is the same, that is, the operation of the string is also supported;
Also note: The contents of the source string have not changed since the substitution operation was performed.
examples are as follows:
String src = new string ("ab43a2c43d"); system.out.println (Src.replace ("3", "F"));=>ab4f2c4fd. system.out.println (Src.replace (' 3 ', ' F '));=>ab4f2c4fd. system.out.println (Src.replaceall ("\\d", "F"));=>abffafcffd. system.out.println (Src.replaceall ("A", "F"));=>fb43fc23d. system.out.println (Src.replacefirst ("\\d," F "));=>abf32c43d system.out.println (Src.replacefirst ("4", "H")); =>abh32c43d.
How to replace "\" in a string with "\ \":
String Msgin; String msgout; Msgout=msgin.replaceall ("\\\\", "\\\\\\\\");
Reason:
' \ ' is an escape character in Java, so it takes two to represent one. For example, System.out.println ("\ \"); only one "\" is printed. But ' \ ' is also an escape character in a regular expression (ReplaceAll's argument is a regular expression) and requires two to represent one. So: \\\\ is converted to \\,\\ by Java and converted to \ by the regular expression.
same
CODE: \\\\\\\\
Java: \\\\
Regex: \ \
There are several ways to replace '/' in a string with ' \ ':
msgout= Msgin.replaceall ("/", "\\\\"); msgout= msgin.replace ("/", "\ \"); msgout= msgin.replace ('/', ' \ \ ');
Java string substitution