当前位置: 首页 > 编程笔记 >

Java字符串replace(),replaceFirst()和replaceAll()方法

谷梁英毅
2023-03-14
本文向大家介绍Java字符串replace(),replaceFirst()和replaceAll()方法,包括了Java字符串replace(),replaceFirst()和replaceAll()方法的使用技巧和注意事项,需要的朋友参考一下

replace()方法

String类的replace()方法接受两个String值-

    list-paddingleft-2">
  • 一个表示要替换的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.

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

 类似资料:
  • 问题内容: 除了以后使用正则表达式之外,java.lang.String 和方法之间有什么区别?对于简单的替代喜欢,更换用 ,有什么不同? 问题答案: 在中,该replace方法可以采用一对或一对(其中是子类,因此很高兴采用一对)。该方法将替换所有出现的或。在另一方面,这两个参数和正则表达式(正则表达式)。使用错误的功能可能会导致细微的错误。

  • 除了后面使用正则表达式之外,java.lang.String的和方法有什么区别?对于简单的替换,如将替换为,有什么区别吗?

  • 问题内容: 线 打印一个反斜杠()。和 打印双反斜杠()。明白了! 但是为什么在以下代码中: 是输出: 代替 毕竟,该方法是将点()替换为()。 有人可以解释一下吗? 问题答案: 使用正则表达式替换字符时,您可以使用反向引用,例如使用匹配项中的分组替换。 但是,这意味着反斜杠是一个特殊字符,因此,如果您实际上想使用反斜杠,则必须对其进行转义。 这意味着在Java字符串中使用它时实际上需要对其进行两

  • 我不明白。我使用了在互联网上找到的所有替换方法,包括我能找到的所有堆栈溢出。我甚至试过\u0024!发生了什么?

  • 本文向大家介绍浅谈java中replace()和replaceAll()的区别,包括了浅谈java中replace()和replaceAll()的区别的使用技巧和注意事项,需要的朋友参考一下 replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSeque

  • 问题内容: 一个非常简单的问题,但这是来自C / C ++人员进入Java的复杂性的。 我知道我可以启动jUnit和自己的一些性能测试来获得答案。但我只是想知道这是否在那里。 在性能方面,String.replaceAll()和Matcher.replaceAll()(在从Regex.Pattern创建的Matcher对象上)之间是否存在已知差异? 此外,两者在高级API方面的区别是什么?(不可变