一、字符串
1.长度
2.格式
for(int i=0; i < s.length() ; i++){
// 指定索引处的字符值
s.charAt(i)
}
二、代码
1.遍历
package nsys.stringStudy;
/**
* @Author: nsys
* @Date: 2021-10-08 20:50
* @Description: 字符串_遍历
*/
public class StringDemo03 {
public static void main(String[] args) {
String s = "nsys2021";
for (int i = 0; i < s.length(); i++) {
// 遍历字符
System.out.println(s.charAt(i));
}
}
}
2.统计
package nsys.stringStudy;
/**
* @Author: nsys
* @Date: 2021-10-08 21:01
* @Description: 字符串_统计字符
*/
public class StringDemo04 {
public static void main(String[] args) {
// 定义需要统计的字符串
String s = "nsysYYDS20211008";
// 统计变量初始化
int bigCount = 0;
int smallCount = 0;
int numCount = 0;
// for循环将字符输出,if判断统计
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'){
bigCount++;
}else if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z'){
smallCount++;
}else if(s.charAt(i) >= '0' && s.charAt(i) <= '9'){
numCount++;
}
}
System.out.println("大写字符数量:" + bigCount);
System.out.println("小写字符数量:" + smallCount);
System.out.println("数字字符数量:" + numCount);
}
}
3.反转
package nsys.stringStudy;
/**
* @Author: nsys
* @Date: 2021-10-08 21:11
* @Description: 字符串_反转
*/
public class StringDemo05 {
public static void main(String[] args) {
String s = "nsys";
// 索引从最后开始 ; 一直到0结束
for (int i = s.length() - 1; i >= 0; i--) {
System.out.print(s.charAt(i));
}
}
}