如何在Java中替换第一次出现的字符串

时间:2022-02-15 16:51:02

I want to replace first occurrence of String in the following.

我想在下面替换第一次出现的String。

  String test = "see Comments, this is for some test, help us"

**If test contains the input as follows it should not replace

**如果测试包含如下输入,则不应替换

  1. See Comments, (with space at the end)
  2. 见评论,(末尾有空格)

  3. See comments,
  4. See Comments**

I want to get the output as follows,

我想获得如下输出,

 Output: this is for some test, help us

Thanks in advance,

提前致谢,

6 个解决方案

#1


66  

You can use replaceFirst(String regex, String replacement) method of String.

您可以使用String的replaceFirst(String regex,String replacement)方法。

#2


14  

Use substring(int beginIndex):

使用substring(int beginIndex):

String test = "see Comments, this is for some test, help us";
String newString = test.substring(test.indexOf(",") + 2);
System.out.println(newString);

OUTPUT:

this is for some test, help us

这是一些测试,帮助我们

#3


11  

You should use already tested and well documented libraries in favor of writing your own code.

您应该使用已经过测试且记录良好的库来支持编写自己的代码。

org.apache.commons.lang3.
  StringUtils.replaceOnce("coast-to-coast", "coast", "") = "-to-coast"

Javadoc

There's even a version that is case insensitive (which is good).

甚至还有一个不区分大小写的版本(这很好)。

Maven

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

Credits

My answer is an augmentation of: https://*.com/a/10861856/714112

我的答案是增加:https://*.com/a/10861856/714112

#4


5  

You can use following statement to replace first occurrence of string.

您可以使用以下语句替换第一次出现的字符串。

String result = input.replaceFirst(Pattern.quote(stringToReplace), stringToReplaceWith);

This link has complete program including test cases.

此链接包含完整的程序,包括测试用例。

#5


1  

You can use following method.

您可以使用以下方法。

public static String replaceFirstOccurrenceOfString(String inputString, String stringToReplace,
        String stringToReplaceWith) {

    int length = stringToReplace.length();
    int inputLength = inputString.length();

    int startingIndexofTheStringToReplace = inputString.indexOf(stringToReplace);

    String finalString = inputString.substring(0, startingIndexofTheStringToReplace) + stringToReplaceWith
            + inputString.substring(startingIndexofTheStringToReplace + length, inputLength);

    return finalString;

}

Following link provide examples for replacing first occurrence of string using with and without regular expressions.

以下链接提供了使用有和没有正则表达式替换第一次出现的字符串的示例。

#6


-1  

Use String replaceFirst to swap the first instance of the delimiter to something unique:

使用String replaceFirst将分隔符的第一个实例交换为唯一的:

String input = "this=that=theother"
String[] arr = input.replaceFirst("=", "==").split('==',-1);
String key = arr[0];
String value = arr[1];
System.out.println(key + " = " + value);

#1


66  

You can use replaceFirst(String regex, String replacement) method of String.

您可以使用String的replaceFirst(String regex,String replacement)方法。

#2


14  

Use substring(int beginIndex):

使用substring(int beginIndex):

String test = "see Comments, this is for some test, help us";
String newString = test.substring(test.indexOf(",") + 2);
System.out.println(newString);

OUTPUT:

this is for some test, help us

这是一些测试,帮助我们

#3


11  

You should use already tested and well documented libraries in favor of writing your own code.

您应该使用已经过测试且记录良好的库来支持编写自己的代码。

org.apache.commons.lang3.
  StringUtils.replaceOnce("coast-to-coast", "coast", "") = "-to-coast"

Javadoc

There's even a version that is case insensitive (which is good).

甚至还有一个不区分大小写的版本(这很好)。

Maven

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

Credits

My answer is an augmentation of: https://*.com/a/10861856/714112

我的答案是增加:https://*.com/a/10861856/714112

#4


5  

You can use following statement to replace first occurrence of string.

您可以使用以下语句替换第一次出现的字符串。

String result = input.replaceFirst(Pattern.quote(stringToReplace), stringToReplaceWith);

This link has complete program including test cases.

此链接包含完整的程序,包括测试用例。

#5


1  

You can use following method.

您可以使用以下方法。

public static String replaceFirstOccurrenceOfString(String inputString, String stringToReplace,
        String stringToReplaceWith) {

    int length = stringToReplace.length();
    int inputLength = inputString.length();

    int startingIndexofTheStringToReplace = inputString.indexOf(stringToReplace);

    String finalString = inputString.substring(0, startingIndexofTheStringToReplace) + stringToReplaceWith
            + inputString.substring(startingIndexofTheStringToReplace + length, inputLength);

    return finalString;

}

Following link provide examples for replacing first occurrence of string using with and without regular expressions.

以下链接提供了使用有和没有正则表达式替换第一次出现的字符串的示例。

#6


-1  

Use String replaceFirst to swap the first instance of the delimiter to something unique:

使用String replaceFirst将分隔符的第一个实例交换为唯一的:

String input = "this=that=theother"
String[] arr = input.replaceFirst("=", "==").split('==',-1);
String key = arr[0];
String value = arr[1];
System.out.println(key + " = " + value);