java - 替换和替换全部的区别

标签 java string replace replaceall

replace 和有什么区别和replaceAll字符串的方法?

在以下示例中,代码给出 same输出。在这两种情况下all字符串的实例被替换。

String first = "Here is a cat There is a cat";
first = first.replace("cat", "dog"); 
System.out.println(first);

输出:

Here is a dog There is a dog

String second = "Here is a cat There is a cat";
second = second.replaceAll("cat", "dog"); 
System.out.println(second);

输出:

Here is a dog There is a dog

最佳答案

 String replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

String replaceAll(String regex, String replacement)

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

来源:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

关于java - 替换和替换全部的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731506/

相关文章:

java - 将 gridLayout 添加到已实现 FlowLayout 的 JLabel 上

Java解释: why String immutable make StringBuffer MORE efficient

Java将字符串转换为逻辑

java - 使用字符串匹配算法返回 null

javascript - Jquery-反斜杠字符

PHP 替换背景图片 url

javascript - 替换子字符串,但如果在标签之间或在带有 javascript 的标签中则不替换

java - 从java中的网页检索URL

java - 如何在静态上使用 getString() ?

Java加载一个DLL,该DLL从JNI中的另一个DLL导出方法