📜  Java中的Java .lang.String.replace()

📅  最后修改于: 2022-05-13 01:55:14.141000             🧑  作者: Mango

Java中的Java .lang.String.replace()

此方法搜索指定的字符串或指定的值,或正则表达式,凭借其具有相同的建议返回一个具有相关字符的新字符串。字符串的 replace() 方法包含三个变体,这里是replace()方法的三个变体。

String.replace() 经常使用 3 种类型,也有助于解决问题的能力。

  • 代替()
  • 全部替换()
  • 替换第一()

#1: String.replace()

此方法返回一个新字符串,该字符串是用新字符替换字符串中所有出现的旧字符而产生的。

句法:

public String replace(char oldch, char newch)

参数:

  • 老字符
  • 新字符

返回类型:

它通过用 newch 替换每次出现的 oldch 来返回从该字符串派生的字符串。

例子

Java
// Java code to demonstrate the
// Working of  replace() method
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initialising custom input string
        String Str = new String("Welcome to geeksforgeeks");
 
        // Using replace to replace characters
        System.out.print("After replacing all o with T : ");
        System.out.println(Str.replace('o', 'T'));
 
        // Using replace to replace characters
        System.out.print("After replacing all e with D : ");
        System.out.println(Str.replace('e', 'D'));
    }
}


Java
// Java Program to demonstrate the
// working of  replaceAll()
 
// Main class
public class rep2 {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initialising String
        String Str = new String("Welcome to geeksforgeeks");
 
        // Display message
        System.out.print("Original String : ");
 
        // Printing original string
        System.out.println(Str);
 
        // Display message
        System.out.print(
            "After replacing regex with replace_str : ");
 
        // Using replaceAll to replace regex
        // with replace_str
        System.out.println(
            Str.replaceAll("(.*)geeks(.*)", "ASTHA TYAGI"));
    }
}


Java
// Java code to demonstrate the
// working of  replaceFirst()
public class rep3 {
   public static void main(String args[]) {
        
      // Initialising String
      String Str = new String("Welcome to geeksforgeeks");
       
      // original string
      System.out.print("Original String : " );
      System.out.println(Str);
       
      // Using replaceFirst to replace regex with replace_str
      // Replaces 1st occurrence of geeks with ASTHA
      System.out.print("After replacing 1st occurrence of regex with replace_str  : " );
      System.out.println(Str.replaceFirst("geeks", "ASTHA"));
       
   }
}


输出
After replacing all o with T : WelcTme tT geeksfTrgeeks
After replacing all e with D : WDlcomD to gDDksforgDDks

#2:字符串替换所有()

此方法用给定的 replace_str 替换与给定正则表达式匹配的字符串的每个子字符串。

句法:

public String replaceAll(String regex, String replace_str)

参数:

  • 此字符串要匹配的正则表达式。
  • 将替换找到的表达式的字符串
Return Value
This method returns the resulting String

例子

Java

// Java Program to demonstrate the
// working of  replaceAll()
 
// Main class
public class rep2 {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initialising String
        String Str = new String("Welcome to geeksforgeeks");
 
        // Display message
        System.out.print("Original String : ");
 
        // Printing original string
        System.out.println(Str);
 
        // Display message
        System.out.print(
            "After replacing regex with replace_str : ");
 
        // Using replaceAll to replace regex
        // with replace_str
        System.out.println(
            Str.replaceAll("(.*)geeks(.*)", "ASTHA TYAGI"));
    }
}
输出
Original String : Welcome to geeksforgeeks
After replacing regex with replace_str : ASTHA TYAGI

#3:字符串替换第一()

此方法用给定的 replace_str 替换此字符串中与给定正则表达式匹配的第一个子字符串。

句法:

public String replaceFirst(String regex, String replace_str)

参数

  • 此字符串要匹配的正则表达式。
  • 将替换找到的表达式的字符串。

返回类型:结果字符串

例子:

Java

// Java code to demonstrate the
// working of  replaceFirst()
public class rep3 {
   public static void main(String args[]) {
        
      // Initialising String
      String Str = new String("Welcome to geeksforgeeks");
       
      // original string
      System.out.print("Original String : " );
      System.out.println(Str);
       
      // Using replaceFirst to replace regex with replace_str
      // Replaces 1st occurrence of geeks with ASTHA
      System.out.print("After replacing 1st occurrence of regex with replace_str  : " );
      System.out.println(Str.replaceFirst("geeks", "ASTHA"));
       
   }
}

输出:

Original String : Welcome to geeksforgeeks
After replacing 1st occurrence of regex with replace_str  : Welcome to ASTHAforgeeks