Replace and replaceall are common replacement characters in Java. Their differences are as follows:
1) The replace parameter is Char and charsequence, which can be used to replace characters or strings (charsequence is the meaning of string sequence, which is a string in White );
2) The replaceall parameter is RegEx, that is, the replacement based on the Rule expression. For example, you can use replaceall ("// D ","*") replace all numeric characters in a string with asterisks;
Replacefirst (), this method is also based on the Rule expression, but unlike replaceall (), it only replaces the string that appears for the first time;
In addition, if the parameters used by replaceall () and replacefirst () are not based on the Rule expression, the effect of replacing the string with Replace () is the same, both support string operations;
Note: After the replacement operation is executed, the content of the source string does not change.
Example:
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 "//":
String msgin;
String msgout;
Msgout = msgin. replaceall ("////","////////");
Cause:
'/' Is an escape character in Java, so two must be used to represent one. For example, system. Out. println ("//"); only one "/" is printed "/". However, '/' is also the Escape Character in the regular expression (The replaceall parameter is the regular expression), and two must be used to represent one. So: //// is converted to // by Java, // is converted to/by a regular expression /.
Similarly
Code :////////
Java :////
RegEx ://
Several Methods to replace '/' with '/' in a string:
Msgout = msgin. replaceall ("/","////");
Msgout = msgin. Replace ("/","//");
Msgout = msgin. Replace ('/','//');