gpt4 book ai didi

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

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

我有一个包含 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/

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