<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 indexOf()`方法 > 原文: [https://beginnersbook.com/2013/12/java-string-indexof-method-example/](https://beginnersbook.com/2013/12/java-string-indexof-method-example/) Java `String indexOf()`方法用于查找給定`String`中指定字符或子字符串的索引。[`String`類](https://beginnersbook.com/2013/12/java-strings/)中有 4 種變體: ## `indexOf()`方法簽名 `int indexOf(int ch)`:返回給定`String`中字符`ch`的第一次出現的索引。 `int indexOf(int ch, int fromIndex)`:返回給定字符串中指定索引`fromIndex`后,字符`ch`的第一次出現的索引。例如,如果像`str.indexOf('A', 20)`那樣調用`indexOf()`方法,那么它將開始在字符串`str`中索引 20 之后查找字符`'A'`。 `int indexOf(String str)`:返回特定`String`中字符串`str`的索引。 `int indexOf(String str, int fromIndex)`:返回給定字符串中指定索引`fromIndex`后,字符串`str`的索引。 如果在特定`String`中找不到指定的`char`/`substring`,則上述所有函數**都返回 -1** 。 ## Java `String indexOf()`方法示例 ```java public class IndexOfExample{ public static void main(String args[]) { String str1 = new String("This is a BeginnersBook tutorial"); String str2 = new String("Beginners"); String str3 = new String("Book"); String str4 = new String("Books"); System.out.println("Index of B in str1: "+str1.indexOf('B')); System.out.println("Index of B in str1 after 15th char:"+str1.indexOf('B', 15)); System.out.println("Index of B in str1 after 30th char:"+str1.indexOf('B', 30)); System.out.println("Index of string str2 in str1:"+str1.indexOf(str2)); System.out.println("Index of str2 after 15th char"+str1.indexOf(str2, 15)); System.out.println("Index of string str3:"+str1.indexOf(str3)); System.out.println("Index of string str4"+str1.indexOf(str4)); System.out.println("Index of hardcoded string:"+str1.indexOf("is")); System.out.println("Index of hardcoded string after 4th char:"+str1.indexOf("is", 4)); } } ``` **輸出:** ```java Index of B in str1: 10 Index of B in str1 after 15th char:19 Index of B in str1 after 30th char:-1 Index of string str2 in str1:10 Index of str2 after 15th char-1 Index of string str3:19 Index of string str4-1 Index of hardcoded string:2 Index of hardcoded string after 4th char:5 ``` ## `indexOf()`方法的另一個例子 讓我們舉一個簡短的例子,我們使用`indexOf()`方法找到給定字符和[子字符串](https://beginnersbook.com/2013/12/java-string-substring-method-example/)的索引。 ```java public class JavaExample { public static void main(String[] args) { String str = "Java String"; char ch = 'J'; char ch2 = 'S'; String subStr = "tri"; int posOfJ = str.indexOf(ch); int posOfS = str.indexOf(ch2); int posOfSubstr = str.indexOf(subStr); System.out.println(posOfJ); System.out.println(posOfS); System.out.println(posOfSubstr); } } ``` **輸出:** ![Java String IndexOf() method example](https://img.kancloud.cn/b1/a0/b1a059455adb1dbb8aded143a93c226f_718x730.jpg)
                  <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>

                              哎呀哎呀视频在线观看