package welcome;
public class 字符串操作 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//关于字符串大小的比较
String s1 = "abc";
String s2 = "abad";
System.out.println(s1.compareTo(s2));
//访问字符串里的字符
System.out.println(s1.charAt(2));
//遍历字符串
for(int i = 0; i < s1.length(); i++)
{
System.out.println(s1.charAt(i));
}
System.out.println();
//遍历子串
String s3 = "012345B67839汉字";
System.out.println(s3.substring(2));
System.out.println(s3.substring(2, 4));
//寻找位置
System.out.println(s3.indexOf('4'));
System.out.println(s3.indexOf('A'));
System.out.println(s3.indexOf("B67"));
System.out.println();
//寻找第二次出现的‘3’
int loc = s3.indexOf('3');
System.out.println(s3.indexOf('3',loc+1));
System.out.println();
//不变的字符串
String s4 = "abcd";
String s5 = s4.toUpperCase();
System.out.println(s4);
System.out.println(s5);
System.exit(0); //注意此种避免错误的方法
}
}
字符串操作
最新推荐文章于 2023-05-19 20:55:18 发布