<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java `String.replaceFirst()`方法示例 > 原文: [https://howtodoinjava.com/java/string/java-string-replacefirst-example/](https://howtodoinjava.com/java/string/java-string-replacefirst-example/) `Java String replaceFirst()`方法用給定的替換子字符串替換找到的第一個子字符串`'regex'`,該子字符串與給定的參數子字符串(或正則表達式)匹配。 子字符串匹配過程從字符串的開頭(索引 0)開始。 ## 1\. `String.replaceFirst(String regex,String replacement)`方法 `String.replaceFirst()`方法使用正則表達式查找子字符串并將其替換為`replacement`子字符串參數。 ```java /** * @param regex - the regular expression to which this string is to be matched * @param replacement - the string to be substituted for the first match * * @return The resulting string after replacement is done */ public String replaceFirst(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceFirst(replacement); } ``` ## 2\. Java `String.replaceFirst()`示例 Java 程序,用新的子字符串替換字符串中給定字符串或正則表達式的第一個匹配項。 在給定的示例中,我將大寫的“`JAVA`”字符串替換為第一次出現的子字符串“`java`”。 ```java public class StringExample { public static void main(String[] args) { String str = "Java says hello world. Java String tutorial"; //Replace first occurrence of substring "Java" with "JAVA" String newStr = str.replaceFirst("Java", "JAVA"); //Replace first occurrence of substring "a" with "A" String regexResult = str.replaceFirst("[a]", "A"); System.out.println(newStr); System.out.println(regexResult); } } ``` 程序輸出。 ```java JAVA says hello world. Java String tutorial JAva says hello world. Java String tutorial ``` ## 3\. 不允許為`null` 兩個方法的參數均不允許使用`null`。 它將拋出`NullPointerException`。 ```java public class StringExample { public static void main(String[] args) { String str = "Java says hello world. Java String tutorial"; String newStr = str.replaceFirst("Java", null); System.out.println(newStr); } } ``` 程序輸出: ```java Exception in thread "main" java.lang.NullPointerException: replacement at java.util.regex.Matcher.replaceFirst(Matcher.java:999) at java.lang.String.replaceFirst(String.java:2165) at com.StringExample.main(StringExample.java:9) ``` 在此示例中,我們學習了**替換 Java 中字符串**中首次出現的字符。 學習愉快! 參考文獻: [Java `String`方法和示例](https://howtodoinjava.com/java-string/) [Java `String` Doc](https://docs.oracle.com/javase/10/docs/api/java/lang/String.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看