<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java - `String substring()`方法 > 原文: [https://beginnersbook.com/2013/12/java-string-substring-method-example/](https://beginnersbook.com/2013/12/java-string-substring-method-example/) 方法`substring()`返回一個新字符串,它是給定字符串的子字符串。 Java [`String`](https://beginnersbook.com/2013/12/java-strings/)`substring()`方法用于根據傳遞的索引獲取給定字符串的子字符串。這種方法有兩種變體。在本指南中,我們將看到如何在示例的幫助下使用此方法。 ## `String substring()`方法變體 我們可以通過兩種方式使用`substring()`方法 - **1.當我們只通過起始指數時:** ```java String substring(int beginIndex) ``` 返回從指定索引開始的子字符串,即`beginIndex`,并擴展到字符串末尾的字符。例如 - `"Chaitanya".substring(2)`將返回`"aitanya"`。`beginIndex`是包含的,這就是索引 2 中出現的字符包含在子字符串中的原因。如果`beginIndex`小于零或大于`String`的長度(`beginIndex < 0 || > String.length()`)拋出`IndexOutOfBoundsException`。 **2.當我們傳遞兩個索引時,起始索引和結束索引:** ```java String substring(int beginIndex, int endIndex) ``` 返回一個新字符串,該字符串是此字符串的子字符串。子字符串從指定的`beginIndex`開始,并擴展到索引`endIndex - 1`處的字符。**因此子字符串的長度是`endIndex-beginIndex`。** 換句話說,你可以說`beginIndex`是包容性的,而`endIndex`在獲取子字符串時是獨占的。 例如 - `"Chaitanya".substring(2,5)`將返回`"ait"`。如果`beginIndex`小于零或`beginIndex > endIndex`或`endIndex`大于`String`的長度,它拋出`IndexOutOfBoundsException`。 ## Java `String substring()`示例 現在我們了解了`substring()`方法的基礎知識,讓我們來看一個[示例](https://beginnersbook.com/2017/09/java-examples/)來理解這個方法的用法。 這里我們有一個字符串`str`,我們使用`substring()`方法找出該字符串的子串。 ```java public class SubStringExample{ public static void main(String args[]) { String str= new String("quick brown fox jumps over the lazy dog"); System.out.println("Substring starting from index 15:"); System.out.println(str.substring(15)); System.out.println("Substring starting from index 15 and ending at 20:"); System.out.println(str.substring(15, 20)); } } ``` **輸出:** ```java Substring starting from index 15: jumps over the lazy dog Substring starting from index 15 and ending at 20: jump ``` **注意:**很多人都很困惑,第二個方法調用中返回的子字符串應該是`" jump"`而不是`" jumps"`,這是因為返回的子字符串長度應該是`endIndex-beginIndex`,在我們的例子中,`beginIndex`是 15,`endIndex`是 20,因此,返回的子串的長度應該是`20-15 = 5`。正確的答案是`" jump"`,因為在它之前有一個空格,所以子串`" jump"`的長度是 5(包括空格)。 為了進一步避免混淆,我正在分享這個方法的另一個例子,這個例子很容易理解。 ## `String substring()`方法的另一個例子 ```java public class JavaExample{ public static void main(String args[]) { String mystring = new String("Lets Learn Java"); /* The index starts with 0, similar to what we see in the arrays * The character at index 0 is s and index 1 is u, since the beginIndex * is inclusive, the substring is starting with char 'u' */ System.out.println("substring(1):"+mystring.substring(1)); /* When we pass both beginIndex and endIndex, the length of returned * substring is always endIndex - beginIndex which is 3-1 =2 in this example * Point to note is that unlike beginIndex, the endIndex is exclusive, that is * why char at index 1 is present in substring while the character at index 3 * is not present. */ System.out.println("substring(1,3):"+mystring.substring(1,3)); } } ``` **輸出:** ![Java String substring method example](https://img.kancloud.cn/01/24/0124c1efb9556d85474628da58c80dd7_1024x695.jpg) **參考:**[Javadoc `substring()`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int))
                  <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>

                              哎呀哎呀视频在线观看