package String类;
public class StringTest {
public static void main(String[] args) {
//不可变字符串意思就是会生成一个新的字符串 不能再原来基础上直接使用
String s = new String("abcdefg");
String s2 = "abcdefg";
String s3 = "ABCDEFG";
System.out.println(s.equals(s2));
//忽略大小写判断字符串是否相等
System.out.println(s.equalsIgnoreCase(s3));
//length()返回当前字符串对象的长度
System.out.println(s.length());
//返回字符串对象中的某个下标的单个字符
char c_a=s.charAt(0);
char c_b=s.charAt((s.length()-1));
System.out.println(c_a);
System.out.println(c_b);
//从头开始找返回所包含的第一个子字符串的索引
int index=s.indexOf("c");
System.out.println(index);
//从最后开始找所包含的第一个子字符串的索引
int lastIndex=s.indexOf("c");
System.out.println(lastIndex);
//字符串替换
String s4=s2.replace("a", "A");
System.out.println(s4);
String s5=s2.replace("ab", "cd");
System.out.println(s5);
//开头或者结尾字符串判断
//判断是否为ab开头的
System.out.println(s.startsWith("ab"));
//判断是否为ee开头的
System.out.println(s.endsWith("ee"));
//截取字符串,注意:截取字符串包头不包尾
String s6 = "abcdefgh".substring(2);
System.out.println(s6);
String s7 = "abcdefgh".substring(2,6);
System.out.println(s7);
//大小写转换
System.out.println("abcde".toUpperCase());//转成大写
System.out.println("Abced".toLowerCase());//转成小写
//删除字符串头部和尾部的空格
String s8=" ab ";
String s9=s8.trim();
System.out.println(s9);
//类型转换
String s10 = "123456";
System.out.println(Integer.valueOf(s10));
}
}
待续补充
运行结果:
true
true
7
a
g
2
2
Abcdefg
cdcdefg
true
false
cdefgh
cdef
ABCDE
abced
ab
123456
javase基础篇学习之String常用方法
最新推荐文章于 2023-02-23 10:48:50 发布