<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 lastIndexOf()`方法 > 原文: [https://beginnersbook.com/2013/12/java-string-lastindexof-method-example/](https://beginnersbook.com/2013/12/java-string-lastindexof-method-example/) 在上一個教程中,我們討論了[`indexOf()`](https://beginnersbook.com/2013/12/java-string-indexof-method-example/)方法,該方法用于找出給定`String`中指定`char`或子字符串的出現。在本教程中,我們將討論`lastIndexOf()`方法,該方法用于找出給定`String`中或字符[子串](https://beginnersbook.com/2013/12/java-string-substring-method-example/)的最后一次出現的索引。 ## Java `String lastIndexOf()`方法簽名 要找出指定字符或字符串的最后一次出現,此方法從字符串末尾開始搜索并從那里向后返回。如果在方法調用期間指定了`fromIndex`,則向后搜索從指定的索引`fromIndex`開始 `int lastIndexOf(int ch)`:返回給定`String`中字符`ch`的最后一次出現。 `int lastIndexOf(int ch, int fromIndex)`:返回`ch`的最后一次出現,它從指定的索引`fromIndex`開始向后看。 `int lastIndexOf(String str)`:返回`String`中`substring str`的最后一次出現。 `int lastIndexOf(String str, int fromIndex)`:返回`str`的最后一次出現,開始從指定的索引`fromIndex`向后搜索。 ## Java `String lastIndexOf()`示例 在下面的例子中,我們在`String str`中搜索幾個給定的字符和一個給定的子字符串。我們正在尋找它們在`String str`中的最后一次出現,這就是我們使用`lastIndexOf()`方法的原因。如果要查找字符串中`char`或子字符串的第一次出現,請改用[`indexOf()`方法](https://beginnersbook.com/2013/12/java-string-indexof-method-example/)。 ```java public class JavaExample { public static void main(String[] args) { String str = "beginnersbook is for beginners"; char ch = 'b'; char ch2 = 's'; String subStr = "beginners"; int posOfB = str.lastIndexOf(ch); int posOfS = str.lastIndexOf(ch2); int posOfSubstr = str.lastIndexOf(subStr); System.out.println(posOfB); System.out.println(posOfS); System.out.println(posOfSubstr); } } ``` **輸出:** ![Java String lastIndexOf() method example](https://img.kancloud.cn/ff/06/ff06ec37e600df1d0528737223f8194d_834x726.jpg) ## `String lastIndexOf()`方法的另一個例子 這里我們將演示在各種情況下使用`lastIndexOf()`方法。我們正在嘗試使用`lastIndexOf()`方法的不同變體,我們提供`fromIndex`,然后該方法從指定的`fromIndex`向后搜索。 ```java public class LastIndexOfExample{ 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("Last 'B' in str1: "+str1.lastIndexOf('B')); System.out.println("Last 'B' in str1 whose index<=15:"+str1.lastIndexOf('B', 15)); System.out.println("Last 'B' in str1 whose index<=30:"+str1.lastIndexOf('B', 30)); System.out.println("Last occurrence of str2 in str1:"+str1.lastIndexOf(str2)); System.out.println("Last occurrence of str2 in str1 before 15:"+str1.lastIndexOf(str2, 15)); System.out.println("Last occurrence of str3 in str1:"+str1.lastIndexOf(str3)); System.out.println("Last occurrence of str4 in str1"+str1.lastIndexOf(str4)); System.out.println("Last occurrence of 'is' in str1:"+str1.lastIndexOf("is")); System.out.println("Last occurrence of 'is' in str1 before 4:"+str1.lastIndexOf("is", 4)); } } ``` **輸出:** ```java Last 'B' in str1: 19 Last 'B' in str1 whose index<=15:10 Last 'B' in str1 whose index<=30:19 Last occurrence of str2 in str1:10 Last occurrence of str2 in str1 before 15:10 Last occurrence of str3 in str1:19 Last occurrence of str4 in str1-1 Last occurrence of 'is' in str1:5 Last occurrence of 'is' in str1 before 4:2 ```
                  <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>

                              哎呀哎呀视频在线观看