Java经典实例:处理单个字符串

时间:2022-06-29 02:00:48

使用for循环和String对象的charAt()方法;或者,使用“for each”循环和String对象的toCharArray()方法。

/**
* Created by Frank
*/
public class StrCharAt {
public static void main(String[] args) {
String a = "A quick bronze fox lept a lazy bovine";
for (int i = 0; i < a.length(); i++) {
System.out.println("Char " + i + " is " + a.charAt(i));
}
}
}
public class ForEachChar {
public static void main(String[] args) {
String s = "Hello Wold";
for (char c : s.toCharArray()) {
System.out.println(c);
}
}
}