Java String replace()、replaceFirst() 和 replaceAll() 方法

javaobject oriented programmingprogramming

replace() 方法

String 类的 replace() 方法接受两个字符串值 −

  • 一个表示要替换的字符串部分(子字符串)。

  • 另一个表示需要替换指定子字符串的字符串。

使用此方法,您可以替换 java 中字符串的一部分。

示例

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String content = sb.toString();
      System.out.println(" 文件内容:\n"+contents);
      System.out.println(" ");
      //用所需单词替换单词
      content = content.replace("Tutorialspoint", "TP");
      System.out.println(" 替换所需单词后的文件内容:\n"+contents);
   }
}

输出

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At TP provide hundreds of technical tutorials for free.

此方法的另一种变体也接受两个字符,分别代表现有字符和一个字符(以相同的顺序),并用整个字符串中的新字符替换旧字符。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String content = sb.toString();
      System.out.println(" ");
      //用所需单词替换该单词
      content = content.replace('T', '#');
      System.out.println("替换所需单词后的文件内容:\n"+contents);
   }
}

输出

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to #utorialspoint. At #utorialspoint provide hundreds of technical tutorials for free.

replaceAll() 方法

String 类的 replaceAll() 方法接受两个字符串(表示正则表达式和替换模式/字符串),并用指定的模式/字符串替换匹配的值。

示例

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class replaceAllExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String content = sb.toString();
      System.out.println("文件内容:\n"+contents);
      System.out.println();
      //用所需单词替换该单词
      content = content.replaceAll("\bTutorialspoint\b", "TP");
      System.out.println("替换所需单词后的文件内容:\n"+contents);
   }
}

输出

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At TP provide hundreds of technical tutorials for free.

replaceFirst() 方法

String 类的 replaceFirst() 方法 replaceFirst (也) 接受两个表示正则表达式和替换字符串的字符串,并用替换字符串替换第一个匹配项。

示例

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String content = sb.toString();
      System.out.println("文件内容:\n"+contents);
      System.out.println(" ");
      //用所需单词替换该单词
      content = content.replaceFirst("Tutorialspoint", "TP");
      System.out.println("替换所需单词后的文件内容:\n"+contents);
   }
}

输出

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At Tutorialspoint provide hundreds of technical tutorials for free.

注意 − 所有这些方法都区分大小写。


相关文章