Computer >> Máy Tính >  >> Lập trình >> Java

Phương thức Java String Replace (), ReplaceFirst () &ReplaceAll ()

Phương thức Replace ()

Replace () phương thức của lớp String chấp nhận hai giá trị String -

  • Một đại diện cho phần của Chuỗi (chuỗi con) sẽ được thay thế.

  • Một chuỗi khác đại diện cho Chuỗi mà bạn cần thay thế chuỗi con đã chỉ định.

Sử dụng phương pháp này, bạn có thể thay thế một phần của chuỗi trong java.

Ví dụ

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 contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replace("Tutorialspoint", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

Đầu ra

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.

Một biến thể khác của phương thức này cũng chấp nhận hai ký tự, đại diện cho ký tự hiện có và một ký tự tương ứng (theo cùng một thứ tự) và thay thế ký tự cũ bằng ký tự mới trong toàn bộ chuỗi.

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 contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replace('T', '#');
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

Đầu ra

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.

Phương thức ReplaceAll ()

ReplaceAll () phương thức của lớp String chấp nhận hai chuỗi đại diện cho một biểu thức chính quy và một mẫu / chuỗi thay thế và thay thế các giá trị đã khớp bằng mẫu / chuỗi được chỉ định.

Ví dụ

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 contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println();
      //Replacing the word with desired one
      contents = contents.replaceAll("\\bTutorialspoint\\b", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

Đầu ra

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.

Phương thức ReplaceFirst ()

ReplaceFirst () phương thức của lớp String (cũng) ReplaceFirst chấp nhận hai chuỗi đại diện cho một biểu thức chính quy và một chuỗi thay thế và thay thế khớp đầu tiên bằng chuỗi thay thế.

Ví dụ

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 contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replaceFirst("Tutorialspoint", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

Đầu ra

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.

Lưu ý - Tất cả các phương pháp này đều phân biệt chữ hoa chữ thường.