Java字符串替换

Java String replace methods are used to replace part of the string with some other string. These string replace methods are sometimes very useful; for example replacing all occurrence of “colour” to “color” in a file.

Java字符串替换方法用于用其他一些字符串替换部分字符串。 这些字符串替换方法有时非常有用。 例如,将文件中所有出现的“颜色”替换为“颜色”。

Java字符串替换 (Java String replace)

Java String replace

Java String class has four methods to replace a substring. Two of these methods accept regular expressions to match and replace part of the string.


Java String类具有四种替换子字符串的方法。 这些方法中的两个接受正则表达式来匹配和替换字符串的一部分。

  1. public String replace(CharSequence target, CharSequence replacement): This method replaces each substring of this string with the replacement string and return it. Note that replacement happens from start of the string towards end of the string. This behaviour can be easily confirmed by below code snippet.
    String str1 = "aaaaa";
    str1 = str1.replace("aa","x");
    System.out.println(str1); //xxa

    public String replace(CharSequence target, CharSequence replacement) :此方法用替换字符串替换此字符串的每个字符串并返回它。 请注意,替换是从字符串的开始到字符串的结尾进行的。 可以通过下面的代码片段轻松确认此行为。
  2. public String replace(char oldChar, char newChar): This method is used to replace all occurrences of oldChar character to newChar character.

    public String replace(char oldChar, char newChar) :此方法用于将所有出现的oldChar字符替换为newChar字符。
  3. public String replaceAll(String regex, String replacement): This is a very useful method because we can pass regex to match and replace with the replacement string.

    public String replaceAll(String regex, String replacement) :这是一个非常有用的方法,因为我们可以传递正则表达式来匹配并替换为替换字符串。
  4. public String replaceFirst(String regex, String replacement): This string replacement method is similar to replaceAll except that it replaces only the first occurrence of the matched regex with the replacement string.

    public String replaceFirst(String regex, String replacement) :此字符串替换方法与replaceAll相似,不同之处在于,它仅使用替换字符串替换匹配的正则表达式的第一个匹配项。

Let’s look into java string replace methods with example code.

让我们看看带有示例代码的java字符串替换方法。

Java String替换字符示例 (Java String replace character example)

One of the popular use case for character replacement is to change a delimiter in a string. For example, below code snippet shows how to change pipe delimiter to comma in the given string.

字符替换的流行用法之一是更改字符串中的定界符。 例如,下面的代码片段显示了如何在给定的字符串中将管道定界符更改为逗号。

package com.journaldev.string;

public class JavaStringReplaceChar {

	public static void main(String[] args) {
		
		String str = "Android|java|python|swift";
		str = str.replace('|', ',');
		System.out.println(str);
	}
}

Java String replace()示例 (Java String replace() example)

Let’s look at java string replace method example to replace target string with another string. I will take user input from java scanner class for source, target and replacement strings.

让我们看一下Java字符串替换方法示例,该示例将目标字符串替换为另一个字符串。 我将从Java扫描程序类获取用户输入的源,目标和替换字符串。

package com.journaldev.string;

import java.util.Scanner;

public class JavaStringReplace {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Source Term:");
		String source = sc.nextLine();

		System.out.println("Enter Search Term:");
		String search = sc.nextLine();

		System.out.println("Enter Replace Term:");
		String replace = sc.nextLine();

		String result = source.replace(search, replace);

		System.out.println("Result = " + result);

		sc.close();
	}

}

Below image illustrates the output of one of the execution of above program.

下图显示了以上程序执行之一的输出。

Java String replaceAll示例 (Java String replaceAll example)

If you notice above program output, target string should be an exact match for replacement. Sometimes it’s not possible because the input string may be different because of case. In this scenario we can use replaceAll method and pass regular expression for case insensitive replacement. Let’s look at a simple program where we will match and replace string with case insensitivity.

如果您注意到上述程序输出,则目标字符串应与替换完全匹配。 有时是不可能的,因为输入字符串可能因大小写而有所不同。 在这种情况下,我们可以使用replaceAll方法并传递不区分大小写的正则表达式。 让我们看一个简单的程序,该程序将以不区分大小写的方式匹配并替换字符串。

package com.journaldev.string;

import java.util.Scanner;

public class JavaStringReplaceAll {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter Source Term:");
		String source = sc.nextLine();

		System.out.println("Enter Search Term:");
		String search = sc.nextLine();
		search = "(?i)"+search;
		
		System.out.println("Enter Replace Term:");
		String replace = sc.nextLine();
		
		
		String result = source.replaceAll(search, replace);
		
		System.out.println("Result = "+result);
		
		sc.close();
	}

}

Did you notice the prefix (?i) to the search term? This is to pass regex to match strings with case insensitive way. Below image shows the output where both “Android” and “android” terms are getting replaced with “Java” because we used replaceAll method.

java string replaceAll example

您是否注意到搜索词的前缀(?i) ? 这是通过不区分大小写的方式传递正则表达式来匹配字符串。 下图显示了由于我们使用replaceAll方法而将“ Android”和“ android”术语都替换为“ Java”的输出。

Java String replace第一个示例 (Java String replaceFirst example)

Java String replaceFirst is used to replace only the first matched regex string with the replacement string. Let’s look at a simple example of String replaceFirst method.

Java字符串replaceFirst用于用替换字符串仅替换第一个匹配的正则表达式字符串。 让我们看一下String replaceFirst方法的一个简单示例。

package com.journaldev.string;

public class JavaStringReplaceFirst {

	public static void main(String[] args) {
		String str = "Hello JournalDev Users";
		
		str = str.replaceFirst("Hello", "Welcome");
		System.out.println(str);
		
		String str1 = "HELLO Java String Tutorial";
		str1 = str1.replaceFirst("(?i)"+"hello", "Welcome to");
		System.out.println(str1);
	}

}

That’s all for java String replace methods with example code.

这就是带有示例代码的java String替换方法的全部内容。

Reference: String API Doc

参考: 字符串API文档

翻译自: https://www.journaldev.com/17988/java-string-replace

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值