📌  相关文章
📜  Java中的 Matcher replaceFirst(String) 方法及示例

📅  最后修改于: 2022-05-13 01:54:37.372000             🧑  作者: Mango

Java中的 Matcher replaceFirst(String) 方法及示例

Matcher 类的replaceFirst()方法表现为追加替换方法。此方法读取输入字符串并将其替换为匹配器字符串中的第一个匹配模式。

句法:

public String replaceFirst(String stringToBeReplaced)

参数:要替换的字符串,即匹配器中要替换的字符串。

返回类型:通过替换字符串构造的带有目标字符串的字符串。

示例 1:

Java
// Java Program to Illustrate replaceFirst() Method
// of Matcher class
 
// Importing required classes
import java.util.regex.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Getting the regex to be checked
        String regex = "(Geeks)";
 
        // Creating a pattern from regex
        Pattern pattern = Pattern.compile(regex);
 
        // Getting the String to be matched
        String stringToBeMatched
            = "GeeksForGeeks Geeks for For Geeks Geek";
 
        // Creating a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
 
        // Displaying the string to be matched
        // before replacing
        System.out.println("Before Replacement: "
                           + stringToBeMatched);
 
        // Getting the string to be replaced
        String stringToBeReplaced = "GFG";
        StringBuilder builder = new StringBuilder();
 
        // Replacing every matched pattern with the target
        // string using replaceFirst() method
        System.out.println(
            "After Replacement: "
            + matcher.replaceFirst(stringToBeReplaced));
    }
}


Java
// Java Program to Illustrate replaceFirst() Method
 
// Importing required classes
import java.util.regex.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Getting the regex to be checked
        String regex = "(FGF)";
 
        // Creating a pattern from regex
        Pattern pattern = Pattern.compile(regex);
 
        // Getting the string to be matched
        String stringToBeMatched = "FGF FGF FGF FGF";
 
        // Creating a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
 
        // Printing string on console
        // before replacement
        System.out.println("Before Replacement: "
                           + stringToBeMatched);
 
        // Getting the string to be replaced
        String stringToBeReplaced = "GFG";
        StringBuilder builder = new StringBuilder();
 
        // Replacing every matched pattern with target
        // string using replaceFirst() method and printing
        // the string to be replaced
        System.out.println(
            "After Replacement: "
            + matcher.replaceFirst(stringToBeReplaced));
    }
}


输出:
Before Replacement: GeeksForGeeks Geeks for For Geeks Geek
After Replacement: GFGForGeeks Geeks for For Geeks Geek

示例 2:

Java

// Java Program to Illustrate replaceFirst() Method
 
// Importing required classes
import java.util.regex.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Getting the regex to be checked
        String regex = "(FGF)";
 
        // Creating a pattern from regex
        Pattern pattern = Pattern.compile(regex);
 
        // Getting the string to be matched
        String stringToBeMatched = "FGF FGF FGF FGF";
 
        // Creating a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
 
        // Printing string on console
        // before replacement
        System.out.println("Before Replacement: "
                           + stringToBeMatched);
 
        // Getting the string to be replaced
        String stringToBeReplaced = "GFG";
        StringBuilder builder = new StringBuilder();
 
        // Replacing every matched pattern with target
        // string using replaceFirst() method and printing
        // the string to be replaced
        System.out.println(
            "After Replacement: "
            + matcher.replaceFirst(stringToBeReplaced));
    }
}
输出:
Before Replacement: FGF FGF FGF FGF
After Replacement: GFG FGF FGF FGF