下面以源代码加注释方式介绍一下Java中于String类型相关的函数的使用方法:
public class TestString {
public static void main(String[] args) {
//1)charAt(下标从0开始) 返回字符串中某一个字符
String s1 = "abcdefg";
System.out.println(s1.charAt(3));//下标从0开始
//2)对象1.compareTo(对象2) 0表示相等;正数:前一个大于后一个;负数:反过来
//对于某一个字符:大小按照unicode码表来决定他们的大小顺序
String s2 = "a";
String s3 = "A";
System.out.println(s2.compareTo(s3));
//如果有多个字符:首相比较首字母,首字母相等,再比较第二个字符。。。。。。以此类推
String s4 = "aaaB";
String s5 = "aaab";
System.out.println(s4.compareTo(s5));
"110".compareTo("20");//20>110;比较首字母
System.out.println("110".compareTo("20"));
//如果前面字符串一样,但后面长度不同,没有可比字符,比长度,长度大,字符串大
"a".compareTo("ab");
System.out.println("比较长度的例子:"+"a".compareTo("ab"));//-1
//3)concat:拼接 不会改动字符串原有内容,结果返回一个新的字符串
String s6 = "aaa";
String s7 = "bbb";
String s8 = s6.concat(s7);//将s6拼接到s7前面
System.out.println(s8);
System.out.println(s6);//拼接不会改变原来内容
String s9 = s6+s7;
System.out.println(s9);//结果一样,但底层实现不一样
System.out.println(s6);
//4)contains 判断字符串是否包含了某个字符串,用于检测字符串敏感词
String s10 = "89aaabbbccc89";
s10.contains("bbb");
System.out.println(s10.contains("bbb"));//true
//XXX
//5)indexOf() 获取某个串在在整个字符串中的下标,从0开始,
// 在整个字符串中不存在,返回值-1
// 从前往后找,只匹配一次
s10.indexOf("89");
System.out.println("下标值:" + s10.indexOf("89"));//只匹配第一次
System.out.println("下标值:" + s10.indexOf("9"));
System.out.println("下标值:" + s10.indexOf("7"));//-1
//aaa.bbb.jpg
//aaa.bbb.zip
//如何查找一个文件的后缀:从后往前找
String s11 = "aaa.bbb.zip";
System.out.println("最后一个小数点的位置:" + s11.lastIndexOf("."));
//6).length()字符串的长度
System.out.println("长度是:" + "323afsa汉字".length());
//7) 判断字符串是否以某个字符开始
System.out.println("Hello,abc".startsWith("H"));
//8) .substring获取字符串中的一个子串(一个参数)
String s12 = "abcdefg";
String s13 = s12.substring(2);//从下标为2开始,即从c开始到最后
System.out.println("截取的结果:" + s13);
String s14 = s12.substring(2,4);
//两个参数:
//参数1:开始位置(包含)
//参数2:结束位置(不包含)
System.out.println("截取的结果:" +s14);//包含开始位置,不包含结束位置
//9)转换大小写
//toUpperCase
//toLowerCase
System.out.println("abCD".toUpperCase());
System.out.println("abCD".toLowerCase());
//10).ltrim(),截取掉首位的空白字符(中间空白不去),用处:
" 123456 ".trim();
System.out.println(" 123 456 ".trim());
//正则表达式(regular expression)
/*
split
matches
replaceAll
*/
String ss1 = "aaa";//常量池
String ss2 = "aaa";//常量池,与ss1是同一个
String ss3 = new String("aaa");//堆中,新创建对象
String ss4 = "aaa" + "bbb";//字面量加法:在编译阶段已经连起来成为:aaabbb
String ss5 = "aaabbb";
String ss6 = "bbb";
String ss7 = ss1 + ss6;//字符串变量加法:
String ss8 = new String("aaa");
//ss1==ss2;//true
//ss1==ss3; //false
//ss4==ss5; //true
//ss5==ss7;//false
//ss3==ss8;//false
System.out.println(ss1==ss2);
System.out.println(ss1==ss3);
System.out.println(ss4==ss5);
System.out.println(ss5==ss7);
System.out.println(ss3==ss8);
//相同字符串内容不应该存储多次,Java这里对字符串类型进行了优化
//方法区组成部分:常量池(串池StringPool)
//字符串字面量 --->加入--->常量池(只会存储一次,不会创建多个对象)
//对于new String ,每次会分配新的内存,存储于堆中
//字符串字面量做加法 String a = "aaa" + "bbb";
// 在编译阶段成为(已经连接起来):aaabbb
//字符串变量加法:
//|-----StringBuffer 字符串的创建器
// |-----与String区别:
// |---String类型一旦创建就不能改动
// |---" aaabbb "=>旧的内容没有变,新生成的
// |---StringBuffer 字符串可以随便改动
//变量相加的底层实现原理:只要出现一个变量就转化为StringBuffer()对象做加法
// 最后会产生新的字符串对象
StringBuffer tmp =new StringBuffer();
tmp.append(ss1);
tmp.append(ss6);//遇到“+”再来一个append
String d1 = tmp.toString();//生成了一个新的字符串对象(堆)
}
}