Java:如何删除两个字符串之间第一次出现的匹配子字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14535221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:34:45  来源:igfitidea点击:

Java: How to Delete the first occurrence of matching substring between 2 strings?

javaregex

提问by InfantPro'Aravind'

If I have two strings .. say

如果我有两个字符串..说

string1="Hello dear c'Lint and dear Bob"

and

string2="dear"

I want to Compare the stringsand deletethe first occurrence of matching substring ..
the result of the above string pairs is:

我想比较字符串删除匹配子字符串的第一次出现..
上述字符串对的结果是:

Hello c'Lint and dear Bob

Hello c'Lint and dear Bob

This is the code I have written which takes input and returns the matching occurence:

这是我编写的代码,它接受输入并返回匹配的出现:

System.out.println("Enter your regex: ");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

String RegEx = bufferRead.readLine();
Pattern pattern = Pattern.compile(RegEx);
System.out.println("Enter input string to search: ");
bufferRead = new BufferedReader(new InputStreamReader(System.in));
Matcher matcher = pattern.matcher(bufferRead.readLine());

boolean found = false;
while (matcher.find()) {
    System.out.println("I found the text:\"" + matcher.group() +
            "\" starting at index \'" +
            matcher.start() + 
            "\' and ending at index \'" + 
            matcher.end() + 
            "\'");
}

回答by Jon Skeet

You could eitheruse:

你可以任意使用:

string result = string1.replaceFirst(Pattern.quote(string2), "");

Or you could avoid regexes entirely:

或者您可以完全避免使用正则表达式:

int index = string1.indexOf(string2);
if (index == -1)
{
    // Not found. What do you want to do?
}
else
{
    String result = string1.substring(0, index) + 
                    string1.substring(index + string2.length());
}

You can report the region here using indexand string2.length()very easily. Of course if you wantto be able to match regular expression patterns, you should use them.

您可以在此处使用indexstring2.length()非常轻松地报告区域。当然,如果您希望能够匹配正则表达式模式,则应该使用它们。

EDIT: As noted in another answer, both of these will remove "dear"from "and_dear_Bob"leaving "and__Bob"- with the underscores representing spaces. So you'll end up with two spaces between words. And it doesn't force the match to be a whole word, either. It does exactly what you described, but it doesn't give you the result you apparently want.

编辑:如另一个答案中所述,这两个都"dear"将从"and_dear_Bob"离开中删除"and__Bob"- 下划线代表空格。所以你最终会在单词之间有两个空格。它也不会强制匹配是一个完整的词。它完全符合您的描述,但它没有给您显然想要的结果。

Edit:First choice of code outputs: Hello c'Lint and dear Bobwhere Hello and c'Lint have two whitespace character in the middle. While this code:

编辑:代码输出的首选:Hello c'Lint and dear Bob其中 Hello 和 c'Lint 中间有两个空格字符。虽然这段代码:

string result = string1.replaceFirst(Pattern.quote(string2+" "), ""));

gets rid of additional whitespace character.

摆脱额外的空白字符。