当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.lang.String.replace()用法及代码示例


这是replace()方法的三个变体。本文将对它们进行全部描述,如下所示:1.字符串replace():此方法返回一个新字符串,该字符串是通过用新字符替换字符串中所有旧字符而产生的。

用法:
public String replace(char oldch, char newch)
参数:
oldch:the old character.
newch :the new character.
Return Value
It returns a string derived from this string by replacing every occurrence of oldch with newch.
// Java code to demonstrate the 
// working of  replace()  
public class rep1 { 
   public static void main(String args[]) { 
         
      // Initialising 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')); 
        
   } 
}

输出:

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

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


用法:
public String replaceAll(String regex, String replace_str)
参数:
regex:the regular expression to which this string is to be matched.
replace_str:the string which would replace found expression.
Return Value
This method returns the resulting String.
// Java code to demonstrate the 
// working of  replaceAll()  
public class rep2 { 
   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 replaceAll to replace regex with replace_str 
      System.out.print("After replacing 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.字符串replaceFirst():此方法用给定的replace_str替换与给定的正则表达式匹配的该字符串的第一个子字符串。

Syntax
public String replaceFirst(String regex, String replace_str)
参数
regex:the regular expression to which this string is to be matched.
replace_str: the string which would replace found expression.
返回值:
This method returns a resulting String.
// 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


相关用法


注:本文由纯净天空筛选整理自 Java.lang.String.replace() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。