The difference between Java---replace and replaceall

Source: Internet
Author: User

At first glance, it literally seems like replace replaces only the first occurrence of the character (influenced by JavaScript), ReplaceAll replaces all the characters, in fact, it is not the same as the use of substitution.


The two are easy to confuse, and are described in detail here.


Let's look at Java_api's instructions first:

Public String replace (char Oldchar,char Newchar)

Replaces the substring of the target sequence of all matching literals of this string with the specified literal substitution sequence. The substitution executes from the beginning of the string toward the end, for example, substituting "B" for "AA" in the string "AAA" to generate "BA" instead of "AB".


Returns a new string that is generated by replacing all OldChar that appear in this string with Newchar. If OldChar does not appear in the character sequence represented by this string object, a reference to this string object is returned. Otherwise, a new string object is created to represent a sequence of characters equal to the sequence of characters represented by this String object, except that each occurrence of the OldChar is replaced by a newchar.


     public string ReplaceAll (String regex, String replacement)

Replaces this string with the given replacement string to match each substring of the given regular expression. The Str.replaceall (Regex, Repl) Form of this method call produces exactly the same result as the following expression:
Pattern.compile (regex). Matcher (str). ReplaceAll (REPL)
Parameters:
regex– a regular expression used to match this string
Returns: The resulting String
Thrown: patternsyntaxexception– If the syntax of the regular expression is invalid.

One thing to note: The contents of the source string have not changed since the substitution operation was performed.


The parameters of Replace are char and charsequence, that is, the substitution of characters can be supported, and the substitution of strings is supported (Charsequence is the meaning of string sequences, which is the string);

The ReplaceAll parameter is the regex, which is based on the substitution of the regular expression, for example: You can change all the numeric characters of a string into asterisks by ReplaceAll ("\\d", "*");

The same point: all replace, that is, a character or string in the source string is replaced by the specified character or string;

Differences: ReplaceAll supports regular expressions, so arguments are parsed (two parameters are all), such as ReplaceAll ("\\d", "*"), and replace does not, replace ("\\d", "*") is a string that replaces "\\d". It does not resolve to a regular.


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;

Use a regular expression just to replace all or replace the first, with ReplaceAll or Replacefirst.


A simple example is 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 (" 4 "," H ") ); =>abh32c43d.


There is also a different point: "\" is an escape character in Java, so it needs to be represented by two. For example, System.out.println ("\ \"); only one "\" is printed. But "\" is also an escape character in a regular expression, which needs to be represented by two. So: \\\\ is converted to \\,\\ by Java and converted to a regular expression, so with ReplaceAll replace "\" for "\ \", you need to use ReplaceAll ("\\\\", "\\\\\\\\"), and replace is replace ("\ \" ,"\\\\")。

If you only want to replace the first occurrence, you can use Replacefirst (), which is also based on the substitution of regular expressions, but unlike replaceall (), only the first occurrence of the string is replaced.

Practical application:

public static void Main (string[] arg) throws Ognlexception {String s = "SDF\\A\\AA";//replace the backslash in S with \system.out.println (s); System.out.println (S.replaceall ("\\\\", "\\\\\\\\")); System.out.println (s.replace ("\ \", "\\\\");}


you can see that both of the above returns the same substitution result.
The key here is that String.replaceall () is used as a parameter with regular expression. However, the string for the Java itself has similar handling for the escape character \. First, Java interprets "\\\\" as a string (containing two char)--"\ \" This is what you see in the JDK's documentation.
Next, because ReplaceAll is a regular expression parameter, "\ \" is interpreted as a regex. For a regex, this represents a character, which is "\". For the back of the 8, the end will be interpreted as "\ \".
In other words, suppose String.replaceall () is a normal string, not a regex, so write the code: string target = Source.replaceall (' \ \ ', ' \\\\ ');


Performance comparison:

If we can determine the string substitution, use replace performance slightly better! If you have a large number of indeterminate strings, replaceall+ will perform better!


(Online collation of ~)

The difference between Java---replace and replaceall

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.