[英]How can I replace two bracket symbols in a string in java?
abcdefghijklmnopqrstuvwxyz [deleted] abcdefghijklmnopqrstuvwxyz [已删除]
The simple solution would be to call replaceAll()
on the string twice:简单的解决方案是在字符串上调用replaceAll()
两次:
theString = theString
.replaceAll("(", ",")
.replaceAll(")", ",");
String theString = "Bob(A,B),Barry(C,D),Bill(E,F)";
theString = theString.replaceAll(PatternQuote(")"), ",").replaceAll(PatternQuote("("), ",");
You are not replacing the (
parentheses, only )
您没有替换(
括号,仅)
I am not sure what you mean by PatternQuote(")")
, from described behaviour I guess it is actually Pattern.quote(")")
.我不确定您所说的PatternQuote(")")
是什么意思,从所描述的行为来看,我猜它实际上是Pattern.quote(")")
。 You can replace both parentheses this way: theString = theString.replaceAll("[\\(\\)]", ",");
您可以这样替换两个括号: theString = theString.replaceAll("[\\(\\)]", ",");
(character class with 2 characters which need to be escaped). (字符 class 需要转义 2 个字符)。
Chjach!查查!
You could try this:你可以试试这个:
String theString = "Bob(A,B),Barry(C,D),Bill(E,F)";
String s = theString.replace("(", ",");
String myString = s.replace(")", ",");
System.out.println(myString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.