如何使用 replaceFirst 来替换 {....} [英] How to use replaceFirst to Replace {....}

查看:24
本文介绍了如何使用 replaceFirst 来替换 {....}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a string which contains xyaahhfhajfahj{adhadh}fsfhgs{sfsf}.

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

Now I want to replace {string} with a space.
I want to replace the curly brackets and the string in it with null.

我想使用 replaceFirst 但我不知道这样做的正则表达式.

I want to use replaceFirst for it but I don't know the regex for doing it.

推荐答案

试试这个:

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);
    }
}

这篇关于如何使用 replaceFirst 来替换 {....}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆