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

后端 未结 9 1762
南笙
南笙 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:09

    You can also use a lambda in this case.

        String s = "xyz";
        IntStream.range(0, s.length()).forEach(i -> {
            char c = s.charAt(i);
        });
    

提交回复
热议问题