构造方法
无参构造 String s = new String();
有参构造 String s = new String(" ");
//判断功能
System.out.println(s.equals(s1));//比较字符串是否相等System.out.println(s.equalsIgnoreCase(s1));//比较字符串内容是否相等(忽略大小写)System.out.println(s.contains("b"));//判断包含与否
System.out.println(s.isEmpty());//判断是否为空
System.out.println(s.startsWith("ab"));//判断是否开头是abSystem.out.println(s.endsWith("bc"));//判断结尾是否是bcSystem.out.println(s1.compareTo(s2));//比较s1和s2的长度
//获取功能
System.out.println(s.length());System.out.println(s.charAt(1));//给索引找内容System.out.println(s.indexOf("b"));//给内容找索引
String s3 = "abcdefghijkddd";System.out.println(s3.indexOf("b", 0));//给内容从指定位置找索引System.out.println(s3.lastIndexOf("d"));//从后往前找
System.out.println(s3.substring(2));//从指定位置截取
String s3 = s1.substring(2,6);//截取指定区间 包含开始位置,不包含结束位置
s.replace("c","C");把所有的c换成C
s.replaceAll("\d","C");第一个是正则表达式,换成C
s.replace("c","C");吧第一个c换成C
本文详细介绍了Java中字符串的基本操作,包括无参和有参构造方法,以及equals、equalsIgnoreCase、contains、isEmpty、startsWith、endsWith等判断功能。此外,还讲解了length、charAt、indexOf、substring等获取和截取字符串的方法,以及replace和replaceAll进行替换的用法。
1291

被折叠的 条评论
为什么被折叠?



