[英]How to replace ( and ) brackets with space in string using java
我在下面的字符串中需要用空格替換(和)
例子1
String str = "Somestring 12with (a lot of braces and commas)";
System.out.println(str.trim().replaceAll(".*\\(|\\).*", ""));
**預期的輸出如下**
"Somestring 12with a lot of braces and commas"
例子2
String str = "Somestring 12with a lot of braces and commas)";
System.out.println(str.trim().replaceAll(".*\\(|\\).*", ""));
預期產量
"Somestring 12with a lot of braces and commas"
總的來說,我需要從字符串中刪除(和)。
您可以使用此正則表達式[()]
例如:
str.replaceAll("[()]", "")
輸入:
Somestring 12with (a lot of braces and commas)
Somestring 12with a lot of braces and commas)
輸出量
Somestring 12with a lot of braces and commas
Somestring 12with a lot of braces and commas
或者,您可以這樣做String newStr = str.trim().replaceAll("\\\\(", "").replaceAll("\\\\)", "");
您的正則表達式將替換之前(
或之后)
所有內容( .*
)
。
您可以使用:
String resultString = subjectString.replaceAll("[()]", "");
要么 :
String resultString = subjectString.replaceAll("\\(|\\)", "");
我會用第一種方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.