gpt4 book ai didi

java - 如何使用java用字符串中的空格替换(和)括号

转载 作者:行者123 更新时间:2023-11-29 08:36:03 25 4
gpt4 key购买 nike

我在下面的字符串中需要用空格替换 ( 和 )

示例 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

关于java - 如何使用java用字符串中的空格替换(和)括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44031329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com