package cn.itcast;
/*
* 遍历字符串中每一个字符
*/
public class Test {
public static void main(String[] args) {
String s = "i love java";
for (int i = 0; i < s.length(); i++) { //遍历字符串
char charAt = s.charAt(i); //通过索引获取到每一个字符
System.out.print(charAt+" ");
}
System.out.println();
char[] charArray = s.toCharArray();//将字符串转成字符数组
for (int i = 0; i < charArray.length; i++) { //遍历数组
char c = charArray[i];
System.out.print(c);
}
}
}
|
|