在Java编程中,如何拆分正则表达式和字符串?

以下示例演示如何使用Matcher类的replaceFirst()方法替换字符中指定的子字符串的首次出现。

package com.yiibai;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplaceFirstOccurrence {
    public static void main(String args[]) {
        Pattern p = Pattern.compile("hello");
        String instring = "hello, hello word.";
        System.out.println("initial String: " + instring);
        Matcher m = p.matcher(instring);
        String tmp = m.replaceFirst("Java");
        System.out.println("String after replacing 1st Match: " + tmp);
    }
}
Java

上述代码示例将产生以下结果 -

initial String: hello, hello word.
String after replacing 1st Match: Java, hello word.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-01
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2021-11-03
相关资源
相似解决方案