java - 如何使用 replaceFirst 替换 {....}

标签 java string replace

我有一个包含 xyaahhfhajfahj{adhadh}fsfhgs{sfsf} 的字符串。

现在我想用空格替换{string}
我想用空替换大括号和其中的字符串。

我想为它使用 replaceFirst 但我不知道执行此操作的正则表达式。

最佳答案

试试这个:

public class TestCls {
    public static void main(String[] args) {
        String str = "xyaahhfhajfahj{adhadh}fsfhgs{sfsf}";
        String str1 = str.replaceAll("\\{[a-zA-z0-9]*\\}", " ");// to replace string within "{" & "}" with " ".
        String str2 = str.replaceFirst("\\{[a-zA-z0-9]*\\}", " ");// to replace first string within "{" & "}" with " ".
        System.out.println(str1);
        System.out.println(str2);
    }
}

关于java - 如何使用 replaceFirst 替换 {....},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5658794/

相关文章:

java - 我使用什么版本的 JPA?

string - 在vc++中将 'System::String ^'转换为 'const char *'

c# - 如何分离String数组数据?

java - ANDROID数据库找不到列_id,即使它是KEY_ID=_id

Java EnumSet 语法帮助

c++ - 如何创建自定义整理器?

jquery - 当该部分可能有多个单词时,如何用另一部分替换 href 的特定部分?

pandas - 如何有条件地替换pandas数据框

macos - 将 ascii 格式的表格转换为 rtf

java - mysql插入查询性能