The difference between replace and ReplaceAll in Java

Source: Internet
Author: User

Replace and replaceall are commonly used substitution characters in Java, and their differences are:

1) The Replace parameter isCharAndcharsequence, which can support the substitution of characters, also support the substitution of strings (charsequence is the meaning of string sequences, in other words, the string);
2) The parameters of the ReplaceAll areRegex, which is the substitution of regular expressions, 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 unlike replaceall (), Replaces only the first occurrence of the string;
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 ('/', ' \ \ ');

The difference between replace and ReplaceAll in Java (RPM)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.