String class API on traversing the related 28

String class API on traversal related

  • public char charAt (int index)
    Returns the char value at the specified index. The index ranges from 0 to length () - 1.
    The first char value is the index sequence 0, the next index 1, and so

  • public int length ()
    Returns the length of the string (number of characters refers)

public class StringDemo01 {
    public static void main(String[] args) {
        String name = "java是世界上最优美的语言";
        System.out.println(name.charAt(0));//j
        System.out.println(name.charAt(1));//a
        System.out.println(name.charAt(4));//是

        System.out.println(name.length());//14
    }
}

String string type of traversal

1, the use of cycle
2, a character of a character string to access it again

public class StringDemo02 {
    public static void main(String[] args) {
        //1、接受一个字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String name = sc.nextLine();

        //2、遍历字符串的每一个字符
        //name = "java最美"
        for(int i = 0;i < name.length();i++){
            char c = name.charAt(i);
            System.out.println(c);
          }

    }
}
Published 34 original articles · won praise 16 · views 276

Guess you like

Origin blog.csdn.net/qq_41005604/article/details/105272763