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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java `String charAt()`方法 > 原文: [https://beginnersbook.com/2013/12/java-string-charat-m??ethod-example/](https://beginnersbook.com/2013/12/java-string-charat-method-example/) Java `String charAt(int index)`方法返回字符串中指定索引處的字符。我們在此方法中傳遞的索引值應介于 0 和(`string`的長度 -1)之間。例如:`s.charAt(0)`將返回實例`s`表示的字符串的第一個字符。如果在`charAt()`方法中傳遞的索引值小于 0 或大于等于字符串的長度(`index < 0 || index >= length()`),則 Java `String charAt`方法拋出[`IndexOutOfBoundsException`](https://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html)。 ## Java `String charAt()`方法示例 讓我們舉一個例子來理解`charAt()`方法的使用。在這個例子中,我們有一個字符串,我們使用`charAt()`方法打印字符串的第 1,第 6,第 12 和第 21 個字符。 ```java public class CharAtExample { public static void main(String args[]) { String str = "Welcome to string handling tutorial"; //This will return the first char of the string char ch1 = str.charAt(0); //This will return the 6th char of the string char ch2 = str.charAt(5); //This will return the 12th char of the string char ch3 = str.charAt(11); //This will return the 21st char of the string char ch4 = str.charAt(20); System.out.println("Character at 0 index is: "+ch1); System.out.println("Character at 5th index is: "+ch2); System.out.println("Character at 11th index is: "+ch3); System.out.println("Character at 20th index is: "+ch4); } } ``` **輸出:** ```java Character at 0 index is: W Character at 5th index is: m Character at 11th index is: s Character at 20th index is: n ``` ## 使用`charAt()`方法時的`IndexOutOfBoundsException` 當我們傳遞負索引或大于`length() - 1`的索引時,`charAt()`方法拋出`IndexOutOfBoundsException`。在下面的示例中,我們在`charAt()`方法中傳遞負索引,讓我們看看我們在輸出中得到了什么。 ```java public class JavaExample { public static void main(String args[]) { String str = "BeginnersBook"; //negative index, method would throw exception char ch = str.charAt(-1); System.out.println(ch); } } ``` **輸出:** ![Java String charAt method](https://img.kancloud.cn/fa/b0/fab01f6edbfbb313036a0dfefa374d6d_1024x464.jpg) ## Java `String charAt()`示例打印字符串的所有字符 為了打印字符串的所有字符,我們運行[`for`循環](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/)從 0 到字符串的長度 -1,并使用`charAt()`方法在循環的每次迭代中顯示字符。 ```java public class JavaExample { public static void main(String args[]) { String str = "BeginnersBook"; for(int i=0; i<=str.length()-1; i++) { System.out.println(str.charAt(i)); } } } ``` 輸出: ```java B e g i n n e r s B o o k ``` ## Java `String charAt()`示例計算字符的出現次數 在此示例中,我們將使用`charAt()`方法計算給定字符串中特定字符的出現次數。這里我們有一個字符串,我們正在計算字符串中字符`'B'`的出現次數。 ```java public class JavaExample { public static void main(String[] args) { String str = "BeginnersBook"; //initialized the counter to 0 int counter = 0; for (int i=0; i<=str.length()-1; i++) { if(str.charAt(i) == 'B') { //increasing the counter value at each occurrence of 'B' counter++; } } System.out.println("Char 'B' occurred "+counter+" times in the string"); } } ``` **輸出:** ![Java String charAt() example](https://img.kancloud.cn/35/4d/354d48ff87cf074906b784b9d3940d3b_1024x662.jpg) #### 參考 [`String charAt()` javadoc](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int))
                  <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>

                              哎呀哎呀视频在线观看