replace()
方法String类的replace()方法接受两个String值-
一个表示要替换的String(子字符串)部分。
另一个代表需要替换指定子字符串的字符串。
使用此方法,您可以替换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 contents = sb.toString(); System.out.println("Contents of the file: \n"+contents); System.out.println(" "); //用所需的单词替换单词 contents = contents.replace("Nhooo", "TP"); System.out.println("Contents of the file after replacing the desired word: \n"+contents); } }
Contents of the file: Hello how are you welcome to Nhooo. At Nhooo 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 contents = sb.toString(); System.out.println("Contents of the file: \n"+contents); System.out.println(" "); //用所需的单词替换单词 contents = contents.replace('T', '#'); System.out.println("Contents of the file after replacing the desired word: \n"+contents); } }
Contents of the file: Hello how are you welcome to Nhooo. At Nhooo 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 contents = sb.toString(); System.out.println("Contents of the file: \n"+contents); System.out.println(); //用所需的单词替换单词 contents = contents.replaceAll("\\bNhooo\\b", "TP"); System.out.println("Contents of the file after replacing the desired word: \n"+contents); } }
Contents of the file: Hello how are you welcome to Nhooo. At Nhooo 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类(也可以是replace类)的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 contents = sb.toString(); System.out.println("Contents of the file: \n"+contents); System.out.println(" "); //用所需的单词替换单词 contents = contents.replaceFirst("Nhooo", "TP"); System.out.println("Contents of the file after replacing the desired word: \n"+contents); } }
Contents of the file: Hello how are you welcome to Nhooo. At Nhooo provide hundreds of technical tutorials for free. Contents of the file after replacing the desired word: Hello how are you welcome to TP. At Nhooo provide hundreds of technical tutorials for free.
注意-所有这些方法都区分大小写。