java如何吧一个String型一个一个的输出
比如 String s=“abcdefg”;
如何输出a,b,c,d,e,f,g
String.charAt()
例如
public class CharAt {
public static void main(String[] args) {
String s = "bejing welcome you"; System.out.println(s.charAt(1)); System.out.println(s.charAt(5)); System.out.println(s.charAt(15));
}
}
的结果就是
e
g
y
String类有两个常用的方法
1.public char charAt(int index):返回指定索引处的char值,
字符串的索引也是从0开始的
类似于数组下标
2.public int length():返回此字符串的长度
●数组的长度: 数组名.length
●字符串的长度:字符串对象.length0
遍历字符串的通用格式
String s="asdfgh";
//这里用 length 来获取字符串长度
for(int i=0; i<s.length(); i++) {
s.charAt(i); // 就是指定索引处的字符值
}
String.charAt