מהי שיטת Java String charAt()?

השיטה Java string class charAt() מחזירה את התו באינדקס שצוין של מחרוזת. אינדקס שצוין זה מסופק כפרמטר והוא נע בין 0 ל-n-1, כאשר n מייצג את אורך המחרוזת. תחביר
public char charAt(int index)
פרמטרים השיטה charAt () מקבלת מספר שלם חיובי מכיוון שהאורך אף פעם לא מתחת ל-0. מחזירה היא תמיד מחזירה ערך char של האינדקס שצוין אבל אם ערך הפרמטר שלילי או מעל אורך המחרוזת אז היא זורק חריגה.

שיטות Java String charAt() דוגמאות

class Main {
  public static void main(String[] args) {
    String str = "Java charAt() method example";

    // picking the A character of the string str at index 9 using charAt() method
    System.out.println(str.charAt(9));

    // picking the ( character of the string str at index 11 using charAt() method
    System.out.println(str.charAt(11));

    // picking the ) character of the string str at index 12 using charAt() method
    System.out.println(str.charAt(12));
  }
}
תְפוּקָה
БA ( )

סיכום

אנו מקווים שבינתיים אתה מבין מהי שיטת מחרוזת Java charAt() וכיצד ליישם אותה. אל תהסס להתאמן ולחזור בכל פעם שתזדקק לסיוע נוסף. למידה מהנה!