从java中的句子中删除短语(不区分大小写)

问题描述 投票:0回答:1

我有一个java问题。在java中,我可以通过哪些方法从下面的字符串中删除短语“

Unidentified Issue:
”?搜索要删除的短语应该是不区分大小写

EUB Error - Recs: Unidentified Issue: Service connection is denied.

所以,基本上:

输入字符串为:

EUB Error - Recs: Unidentified Issue: Service connection is denied.

输出字符串应该是:

EUB Error - Recs: Service connection is denied.

任何帮助将不胜感激!

提前非常感谢您!

java regex
1个回答
0
投票

java 的一个好工具是

replaceAll()
使用正则表达式的方法来删除短语“Unidentified Issue:”。

public class StackOverflowHelp{
    public static void main(String[] args) {
        String input = "EUB Error - Recs: Unidentified Issue: Service connection is denied.";
        String output = input.replaceAll("(?i)Unidentified Issue:", "");
        System.out.println(output);
    }
}

(?i)
-> 使正则表达式不区分大小写。
replaceAll()
-> 查找第一个参数的每个实例,然后用空字符串线性替换所有出现的给定正则表达式。

线性意味着从索引 0 -> 结束

此代码应输出:

EUB Error - Recs:  Service connection is denied.

因此,应从输入字符串中删除短语“Unidentified Issue:”。

© www.soinside.com 2019 - 2024. All rights reserved.