一、String类和StringBuilder类和StringBuffer类的区别:
String 对象创建后则不能被修改,是不可变的,所谓的修改其实是创建了新的对象,所指向的内存空间不同。因此使用String类时,程序运行时会额外创建对象,当频繁操作字符串时,就会额外产生很多临时变量。使用 StringBuilder 或 StringBuffer 就可以避免这个问题。
至于 StringBuilder 和StringBuffer ,它们基本相似,不同之处,StringBuffer 是线程安全的,而 StringBuilder 则没有实现线程安全功能,所以性能略高。因此一般情况下,如果需要创建一个内容可变的字符串对象,应优先考虑使用 StringBuilder 类。
二、String类
public class HelloWorld {
//==: 判断两个字符串在内存中首地址是否相同,即判断是否是同一个字符串对象
//equals(): 比较存储在两个字符串对象中的内容是否一致
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
String s4 = new String("Hello");
// Hello为常量字符串,多次出现时会被编译器优化,只创建一个对象(所以s1与s2指同一个对象)
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1 == s4);
System.out.println((s3 == s4) +"\n");
String s5 = "I love " + s1;
System.out.println(s5+"\n");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4));
System.out.println(s3.equals(s4));
}
}
结果:
true
false
false
falseI love Hello
true
true
true
true
三、String类的常用操作:
package tcy01;
public class HelloWorld {
public static void main(String[] args) {
String str = "I will check the method About String";//关于String的方法
System.out.println("是否以 ' String' 结尾:"+str.endsWith(" String"));
System.out.println("是否以 'I will'字符串开始:"+str.startsWith("I will"));
System.out.println("str是否包含 'check the method ' 字符序列:"+str.contains("check the method "));
System.out.println("字符串是否为空:"+str.isEmpty()+"\n");
System.out.println("字符串中第一个字符:"+str.charAt(0));
System.out.println("字符串中第三个字符:"+str.charAt(2));
System.out.println("字符串中t第一次出现的位置:"+str.indexOf('t'));
System.out.println("字符串中t最后一次出现的位置:"+str.lastIndexOf('t')+"\n");
System.out.println("变小写:"+str.toLowerCase());
System.out.println("变大写:"+str.toUpperCase()+"\n");
System.out.println("将check替换成learn:"+str.replace("check","learn"));
System.out.println("去除空格:"+str.trim());
System.out.println("从第3个字符截取到第10个字符:"+str.substring(2,10)+"\n");
System.out.println("去除空格:"+str.concat("add啦啦啦"));
System.out.println("去除空格:"+str+"\n");//这里也可以看出str并没有变,str.concat("add")是一个新的对象
System.out.println("将字符串转换为字符数组:");
char[] arr=str.toCharArray();
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+"-");
}
System.out.println();
System.out.println("将字符串转换为字符串数组:");
String[] strArray=str.split(" ");
for(int i=0;i<strArray.length;i++){
System.out.print(strArray[i]+"--");
}
System.out.println("\n");
System.out.println("返回9的字符串表示形式:"+str.valueOf(arr)+"\n");
//巩固练习,检验文件名,邮箱名是否正确
String fileName = "HelloWorld.java";// Java文件名 ,合法的文件名应该以.java结尾
String email = "laurenyang@imooc.com";// 邮箱,合法的邮箱名中至少要包含"@", 并且"@"是在"."之前
//判断.java文件名是否正确:
int index = fileName.lastIndexOf('.');//获取文件名中最后一次出现"."号的位置
String prefix =fileName.substring(index+1,fileName.length());// 获取文件的后缀
if ( index!=-1 && index!=0 && prefix.equals("java")) {// 判断必须包含"."号,且不能出现在首位,同时后缀名为"java"
System.out.println("Java文件名正确");
} else {
System.out.println("Java文件名无效");
}
// 判断邮箱格式是否正确:
int index2 = email.indexOf('@');// 获取邮箱中"@"符号的位置
int index3 = email.indexOf('.');// 获取邮箱中"."号的位置
if (index2 != -1 && index3 > index2) {// 判断必须包含"@"符号,且"@"必须在"."之前
System.out.println("邮箱格式正确");
} else {
System.out.println("邮箱格式无效");
}
}
}
结果:
是否以 ’ String’ 结尾:true
是否以 ‘I will’字符串开始:true
str是否包含 ‘check the method ’ 字符序列:true
字符串是否为空:false字符串中第一个字符:I
字符串中第三个字符:w
字符串中t第一次出现的位置:13
字符串中t最后一次出现的位置:31变小写:i will check the method about string
变大写:I WILL CHECK THE METHOD ABOUT STRING将check替换成learn:I will learn the method About String
去除空格:I will check the method About String
从第3个字符截取到第10个字符:will che去除空格:I will check the method About Stringadd啦啦啦
去除空格:I will check the method About String将字符串转换为字符数组:
I- -w-i-l-l- -c-h-e-c-k- -t-h-e- -m-e-t-h-o-d- -A-b-o-u-t- -S-t-r-i-n-g-
将字符串转换为字符串数组:
I–will–check–the–method–About–String–返回9的字符串表示形式:I will check the method About String
Java文件名正确
邮箱格式正确
四、StringBuilder类
1、StringBuilder类的建立:
public class StringBuildlei {
public static void main(String[] args){
StringBuilder str = new StringBuilder("StringBuilder建立的新的字符串对象");
System.out.println(str);
}
}
//结果:StringBuilder建立的新的字符串对象
1、StringBuilder类的常用操作:
package tcy01;
public class StringBuildlei {
public static void main(String[] args){
StringBuilder str = new StringBuilder("StringBuilderlalahi字符串对象");
System.out.println("原始字符串---------:"+str);
System.out.println("添加-----------------");
System.out.println("append添加结果:"+str.append("我是后面加上的"));
System.out.println("insert添加结果:"+str.insert(2,"从2加上")+"\n");
System.out.println("删除-----------------");
System.out.println("delete指定范围删除:"+str.delete(1,4));
System.out.println("deleteCharAt()指定位置删除:"+str.deleteCharAt(4)+"\n");
System.out.println("修改-----------------");
//System.out.println("setCharAt修改指定位置字符:"+ str.setCharAt(1,'C'));//StringBuffer中可以这样用
System.out.println("replace()替换指定位置的字符(串):"+str.replace(1,3,"change"));
System.out.println("reverse()字符串反转:"+str.reverse()+"\n");
// 从后往前每隔三位插入逗号
for(int i=str.length()-3;i>0;i-=3){
str.insert(i,',');
}
}
}
结果:
原始字符串———:StringBuilderlalahi字符串对象
添加—————–
append添加结果:StringBuilderlalahi字符串对象我是后面加上的
insert添加结果:St从2加上ringBuilderlalahi字符串对象我是后面加上的删除—————–
delete指定范围删除:S加上ringBuilderlalahi字符串对象我是后面加上的
deleteCharAt()指定位置删除:S加上rngBuilderlalahi字符串对象我是后面加上的修改—————–
replace()替换指定位置的字符(串):SchangerngBuilderlalahi字符串对象我是后面加上的
reverse()字符串反转:的上加面后是我象对串符字ihalalredliuBgnregnahcS