共計 380 個字符,預計需要花費 1 分鐘才能閱讀完成。
可以使用以下方法來遍歷字符串的每個字符:
- 使用 for 循環(huán)遍歷字符串的每個字符:
String str = "Hello";
for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);
System.out.println(c);
}
- 使用增強型 for 循環(huán)遍歷字符串的每個字符:
String str = "Hello";
for (char c : str.toCharArray()) {System.out.println(c);
}
- 使用 Java 8 中的 Stream API 遍歷字符串的每個字符:
String str = "Hello";
str.chars().mapToObj(c -> (char) c).forEach(System.out::println);
以上這些方法都可以用來遍歷字符串的每個字符,選擇其中一種方法即可。
丸趣 TV 網 – 提供最優(yōu)質的資源集合!
正文完