简体   繁体   English

关于java,String.replaceAll

[英]about java, String.replaceAll

I've been trying to solve this problem, i did a research a little about the replaceAll method and it seems that it uses a regular expression. 我一直在尝试解决这个问题,我对replaceAll方法做了一些研究,似乎它使用了正则表达式。 But i never heard of any regular expression that contains '.' 但我从未听说过任何包含“。”的正则表达式。 character. 字符。 This is the code i've been using: 这是我一直在使用的代码:

System.out.println(parsed[1]);
myStatus = parsed[1].replaceAll("...", " ");
System.out.println("new: " + myStatus);
status.setText(myStatus);

Output result is: 输出结果是:

old...string new: 旧...字符串新:

If you want to replace the literal String "..." (three dots), either: 如果要替换文字字符串"..." (三个点),请执行以下任一操作:

  • use replace("...", " ") , which does not use regular expressions 使用replace("...", " ") 使用正则表达式
  • use replaceAll("\\\\.{3}", " ") , which is how you specify a literal dot in regex 使用replaceAll("\\\\.{3}", " ") ,这是您在正则表达式中指定文字点的方式

Unless you need to use replaceAll() (because some implementation you are calling uses it), use replace() 除非您需要使用replaceAll() (因为您正在调用的某些实现都使用它),否则请使用replace()

Edited: 编辑:

Thanks Louis \\\\.{3} is simpler than \\\\.\\\\.\\\\. 感谢路易 \\\\.{3} 简单的\\\\.\\\\.\\\\. (doh!) (喔!)

What your call is actually doing is replacing any group of 3 characters to a space. 您的电话实际上正在执行的操作是将任何3个字符组替换为一个空格。 Thus, the string "old...string" would become 4 spaces. 因此,字符串"old...string"将变为4个空格。 You would require to escape the dots or define a character class quantifier , as they are predefined characters . 您将需要转义点或定义字符类 量词 ,因为它们是预定义字符

Something like 就像是

myStatus = parsed[1].replaceAll("[.]{3}", " ");

Note : You can test your regular expressions for Java here . 注意 :您可以在此处测试Java的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM