Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java replaceAll()与replaceAll(…,Matcher.quoteReplacement)_Java_Matcher_Replaceall - Fatal编程技术网

Java replaceAll()与replaceAll(…,Matcher.quoteReplacement)

Java replaceAll()与replaceAll(…,Matcher.quoteReplacement),java,matcher,replaceall,Java,Matcher,Replaceall,你好:D简短的问题:两者之间的区别是什么 String geb = tf_datum.getText(); String sep = ""; //sep is short for seperator geb = geb.replaceAll("\\.", sep); geb = geb.replaceAll("\\,", sep); geb = geb.replaceAll("\\-", sep); geb = geb.replaceAll("\\ ", sep);` 及 因为两者都在发挥

你好:D简短的问题:两者之间的区别是什么

String geb = tf_datum.getText();
String sep = ""; //sep is short for seperator

geb = geb.replaceAll("\\.", sep);
geb = geb.replaceAll("\\,", sep); 
geb = geb.replaceAll("\\-", sep);
geb = geb.replaceAll("\\ ", sep);`

因为两者都在发挥作用。我试图理解每种方法(在第二种方法中)并将其组合在一起,但没有任何意义。 如果有人能帮助我,那就太好了!
谢谢。:)(我还发现了另一个问题,但他没有在replaceAll()中使用Matcher.quote()…因此,如果它是相同的,我就不是舒尔)

在Matcher案例中,您正在做一些不必要的额外工作,但它仍然有效地做着同样的事情(但我会假设你付出了效率的代价,尽管在这种情况下它可以忽略不计)

在第一种情况下,您执行
geb.replaceAll(“\\”,“)
。因此您的意思是使用
geb
,并将每个句点基本上替换为“nothing”

在第二种情况下,您执行
geb.replaceAll(\\”,Matcher.quoteReplacement(sep))
。现在,您的意思是,使用
geb
并用Matcher.quoteReplacement(“”)的返回值替换每个句点。在本例中,Matcher.quoteReplacement返回的“”正是您输入的内容。因此,它本质上是一个不需要的额外/无用的呼叫。请查看Matcher.quoteReplacement的文档:

对于String.replaceAll,请单击此处:

但这里提到的一件事是使用引号替换来抑制控制字符的特殊含义,如“\”和“$”。因此,如果希望替换字符串(replaceAll的第二个参数)表现为文字字符而不是控制字符,则只需使用该参数

同样值得注意的是,您可以在一个正则表达式中完成这一切,比如
geb.replaceAll(“[\-\\\,\\.\\s]”,”)。我认为还有比这更好的方法,但我的正则表达式并不好。

谢谢:)这很有意义,你的带有“geb.replaceAll(“[\-\\,\\.\\s],”);”的设备正在工作,代码现在看起来好多了!
String geb = tf_datum.getText();

String sep = "";
geb = geb.replaceAll("\\.", Matcher.quoteReplacement(sep));
geb = geb.replaceAll("\\,", Matcher.quoteReplacement(sep)); 
geb = geb.replaceAll("\\-", Matcher.quoteReplacement(sep));
geb = geb.replaceAll("\\ ", Matcher.quoteReplacement(sep));