Difference between String replace() and replaceAll

2019-01-01 08:33发布

What's the difference between java.lang.String 's replace() and replaceAll() methods, other than later uses regex? For simple substitutions like, replace . with / , is there any difference?

12条回答
梦醉为红颜
2楼-- · 2019-01-01 08:49

Q: What's the difference between the java.lang.String methods replace() and replaceAll(), other than that the later uses regex.

A: Just the regex. They both replace all :)

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

PS:

There's also a replaceFirst() (which takes a regex)

查看更多
零度萤火
3楼-- · 2019-01-01 08:54

The replace() method is overloaded to accept both a primitive char and a CharSequence as arguments.

Now as far as the performance is concerned, the replace() method is a bit faster than replaceAll() because the latter first compiles the regex pattern and then matches before finally replacing whereas the former simply matches for the provided argument and replaces.

Since we know the regex pattern matching is a bit more complex and consequently slower, then preferring replace() over replaceAll() is suggested whenever possible.

For example, for simple substitutions like you mentioned, it is better to use:

replace('.', '\\');

instead of:

replaceAll("\\.", "\\\\");

Note: the above conversion method arguments are system-dependent.

查看更多
余生无你
4楼-- · 2019-01-01 08:55
String replace(char oldChar, char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

String replaceAll(String regex, String replacement

Replaces each substring of this string that matches the given regular expression with the given replacement.

查看更多
心情的温度
5楼-- · 2019-01-01 08:55

As alluded to in wickeD's answer, with replaceAll the replacement string is handled differently between replace and replaceAll. I expected a[3] and a[4] to have the same value, but they are different.

public static void main(String[] args) {
    String[] a = new String[5];
    a[0] = "\\";
    a[1] = "X";
    a[2] = a[0] + a[1];
    a[3] = a[1].replaceAll("X", a[0] + "X");
    a[4] = a[1].replace("X", a[0] + "X");

    for (String s : a) {
        System.out.println(s + "\t" + s.length());
    }
}

The output of this is:

\   1
X   1
\X  2
X   1
\X  2

This is different from perl where the replacement does not require the extra level of escaping:

#!/bin/perl
$esc = "\\";
$s = "X";

$s =~ s/X/${esc}X/;
print "$s " . length($s) . "\n";

which prints \X 2

This can be quite a nuisance, as when trying to use the value returned by java.sql.DatabaseMetaData.getSearchStringEscape() with replaceAll().

查看更多
柔情千种
6楼-- · 2019-01-01 08:59

replace and replaceAll change the string and char in all the words but replaceAll supports regex (Regular Expression). There is also replaceFirst which is like replaceAll in that they both support regex and the both change strings and chars, the difference between them is that when you use replaceFirst with regex it replaces the first regex ONLY.

// Java_codes_for_more_explanation ; 

String name1 = "Omar Ahmed Hafez" ;

name1 = name1.replace("Omar", "Ahmed");

System.out.println(name1);

/////////////////////////////////////////////

String name2 = "Omar Ahmed Hafez" ;

name2 = name2.replaceAll("\\s", "-");

// The first parameter("\\W") is regex and it mean replace #ALL
// space by "-" and it change All regex in the line as its replaceALL

System.out.println(name2);

////////////////////////////////////////////

String name3 = "Omar Ahmed Hafez" ;

name3 = name3.replaceFirst ("\\s", "-");

// The first parameter("\\W") is regex and it mean replace #FIRST
// space ONLT by "-" (as //it replaceFirst so it replace the first
// regex only :)  

System.out.println(name3);

And output:

Ahmed Ahmed Hafez

Omar-Ahmed-Hafez
查看更多
残风、尘缘若梦
7楼-- · 2019-01-01 09:05

There are two replace() method in Java, one of them takes character as first parameter and other takes CharSequence (which is super Interface for String, Stringbuffer, etc) as first parameter. Both these methods replaces all occurrences of char or CharSequence with the value you provide in 2nd parameter.

ReplaceAll method takes Regular expression as first parameter, so you need to give it some Regex and the matched content will be replaced by the String you pass in 2nd parameter.

For complete difference between replace()and replaceAll() method you can refer here Difference between replace(), replaceAll() and replaceFirst() method in Java String

查看更多
登录 后发表回答