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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java `String.substring()`方法示例 > 原文: [https://howtodoinjava.com/java/string/java-string-substring-example/](https://howtodoinjava.com/java/string/java-string-substring-example/) Java [字符串](https://howtodoinjava.com/java-string/)`substring()`方法返回一個新字符串,該字符串是該字符串的子字符串。 `substring()`方法是重載方法,有兩種變體: 1. **`String substring(int beginIndex)`** 2. **`String substring(int beginIndex, int endIndex)`** 其中**方法參數**為: * **`beginIndex`** – 起始索引,包括端點。 * **`endIndex`** – 結束索引,不包括。 ## 1\. Java `String.substring(int beginIndex)`示例 它返回一個字符串,該字符串是該字符串的子字符串。 子字符串從指定`'beginIndex'`處的字符開始,到字符串的末尾。 1. 請注意,索引從“0”開始,指的是字符串的第一個字符。 2. 如果參數索引不是有效索引,則方法將引發`StringIndexOutOfBoundsException`錯誤。 有效索引始終大于或等于零; 小于或等于字符串的長度。 > `0 >=`有效索引`<=`字符串的長度 Java 程序從給定索引獲取字符串的子字符串。 ```java public class StringExample { public static void main(String[] args) { String blogName = "howtodoinjava.com"; System.out.println(blogName.substring(3)); //todoinjava.com System.out.println("hello world".substring(6)); //world } } ``` 程序輸出。 ```java todoinjava.com world ``` ## 2\. Java `String.substring(int beginIndex, int endIndex)`示例 它返回一個字符串,該字符串是該字符串的子字符串。 子字符串從指定`'beginIndex'`(**包括**)的字符開始,到`'endIndex'`(**排除**)位置。 Java 程序,用于從給定的開始索引到結束索引獲取字符串的子字符串。 ```java public class StringExample { public static void main(String[] args) { String blogName = "howtodoinjava.com"; System.out.println(blogName.substring(14, blogName.length())); //com System.out.println("hello world".substring(6,9)); //wor System.out.println("0123456789".substring(3, 7)); //3456 } } ``` 程序輸出: ```java com wor 3456 ``` 在最后一條語句中,示例更有意義,因為每個字符都表示其在字符串中的索引位置。 第 0 個索引為“0”,第一個索引為“1”,依此類推,直到第 9 個位置為“9”。 當我們找到從索引 3 到索引 7 的子字符串時,我們得到字符串`3456`。 包括開始索引,排除了結束索引“7”。 學習愉快! 參考文獻: [Java `String`文檔](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>

                              哎呀哎呀视频在线观看