How do I apply the for-each loop to every character in a String?

后端 未结 9 1730
南笙
南笙 2020-11-30 17:32

So I want to iterate for each character in a string.

So I thought:

for (char c : \"xyz\")

but I get a compiler error:



        
9条回答
  •  一整个雨季
    2020-11-30 18:18

    For Travers an String you can also use charAt() with the string.

    like :

    String str = "xyz"; // given String
    char st = str.charAt(0); // for example we take 0 index element 
    System.out.println(st); // print the char at 0 index 
    

    charAt() is method of string handling in java which help to Travers the string for specific character.

提交回复
热议问题