博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java字符串替换
阅读量:2534 次
发布时间:2019-05-11

本文共 5615 字,大约阅读时间需要 18 分钟。

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 to match and replace part of the string.

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

  1. public String replace(CharSequence target, CharSequence replacement): This method replaces each 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 class for source, target and replacement strings.

让我们看一下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:

参考:

翻译自:

转载地址:http://gumzd.baihongyu.com/

你可能感兴趣的文章
window7修改hosts文件
查看>>
【Leetcode_easy】720. Longest Word in Dictionary
查看>>
地铁时光机第一阶段冲刺一
查看>>
Code Smell那么多,应该先改哪一个?
查看>>
站立会议02(一期)
查看>>
oracle数据库导入导出问题
查看>>
Android中的动画
查看>>
LeetCode 119 Pascal's Triangle II
查看>>
【Noip2015pj】求和
查看>>
深入理解JavaScript——闭包
查看>>
C#-WebForm-css box-shadow 给边框添加阴影效果
查看>>
objective-c 编程总结(第七篇)运行时操作 - 动态属性
查看>>
C_数据结构_链表
查看>>
kettle-连接控件
查看>>
Coursera--Neural Networks and Deep Learning Week 2
查看>>
C#中的委托和事件(续)【来自张子扬】
查看>>
机器学习部分国内牛人
查看>>
模拟Sping MVC
查看>>
Luogu 3261 [JLOI2015]城池攻占
查看>>
java修饰符
查看>>