文章目录
1.认识String类
1.1String的创建
1)String str=“hello”
2)String str=new String(“hello”);
3)通过char[]或者byte[]的方式来构造字符串
char [] array={'a','b','c'};
String str=new String(array);
1.2String的比较(相等)
1)==比较对象的身份(比较两个引用中保存的地址是否相同/比较两个引用是否指向同一个对象)在这里需要注意的是,在Java中,所有引用类型使用两个等号比较的是身份。
2)equals方法比较两个字符串的内容。
1.3字符串常量池
池是一种重要的思想方法,把一些经常频繁使用的对象提前创建好保存起来,以备随时去使用。
1.4字符串是不可变对象
String 内部持有的char [] value数组的内容不能在内外部发生。(封装的体现),不可变对象有如下优点:
1)更加方便的放入池中
2)hashCode也不可变
3)线程安全更有保证
2.字符数组,字节数组,字符串的相互转换
1)字节数组/字符数组转换为String:
使用String的构造方法即可
char [] array={'a','b','c'};
String str=new String(array);
2)String转换为字符数组,用toCharArray方法完成
转换得到了一个新的字符数组
public class Func {
public static void main(String[] args) {
String str=new String("hello");
char [] data=str.toCharArray();
for (int i=0;i<data.length;i++){
System.out.print(data[i]+" ");
}
}
}
3)String转换为字节数组,使用getBytes方法完成
这个的转换也会得到一个新的字节数组:
public class Func {
public static void main(String[] args) {
String str=new String("hello");
byte [] data=str.getBytes();
for(int i=0;i<data.length;i++){
System.out.println(data[i]);
}
}
}
3.字符串的常见操作
3.1字符串的比较
1)比较相等 ==/equals(上述已经说明)
2)比较大小
字符串比较大小的规则是什么呢?字典序,先比较首个字符的大小(看首字符的unicode码值的大小,如果首个字符能够分出大小,两个字符的大小关系就确定了,如果首个字符不能分出大小,继续比较下一个字符,以此类推,直到比较完所有的字符,如果还没分出大小,则两个字符串相等)
public class Func {
public static void main(String[] args) {
String str=new String("hello");
String str2="hallo";
//compareTo返回的是一个整数(int)
//str比str2小,返回的是一个<0的结果
//str比str2大,返回的是一个>0的结果
//str和str2相等,返回的结果为0
int result=str.compareTo(str2);
System.out.println(result);
}
}
3)不区分大小写的比较:compareToIgnoreCase
public class Func {
public static void main(String[] args) {
String str=new String("hello");
String str2="Hello";
//compareTo返回的是一个整数(int)
//str比str2小,返回的是一个<0的结果
//str比str2大,返回的是一个>0的结果
//str和str2相等,返回的结果为0
// int result=str.compareTo(str2);
int result=str.compareToIgnoreCase(str2);
System.out.println(result);
}
}
3.2字符串查找
1)contains 判断一个字符串是否存在
public class Func {
public static void main(String[] args) {
String str1="hello world";
String str2="world";
System.out.println(str1.contains(str2));
}
}
2)indexOf:indexOf返回了子字符串的起始位置的下标,如果存在多个子字符串,返回的是最左边结果的下标。(从左往右查找的,找到第一个结果就直接返回),如果找不到就直接返回-1。
public class Func {
public static void main(String[] args) {
String str1="hello world world";
String str2="world";
System.out.println(str1.indexOf(str2));
}
}
3)lastIndextOf:从后往前查找:
public class Func {
public static void main(String[] args) {
String str1="hello world world";
String str2="world";
System.out.println(str1.lastIndexOf(str2));
//打印结果为12
4)startWith和endWith是判定以XXX开头/结尾的,与indexOf用法相类似。
典型用法:
1.判断某个链接的协议类型,会使用startsWith;
2.判断某个文件的类型,会使用endsWith判断扩展名。
3.3字符串的替换
字符串替换:由于String是不可变对象,针对字符串的替换操作,并没有修改字符串的本身,而是生成了一个新的字符串。
public class Func {
public static void main(String[] args) {
String str1="hello world world";
String str2="world";
String result=str1.replaceAll(str2,"Java");
System.out.println(result);
System.out.println(str1);
}
}
replaceFirst与replaceAll用法类似。
3.4字符串拆分/切分
按照一定的分割符,把字符串分成几个部分,把这些部分放到一个数组里。
public class Func {
public static void main(String[] args) {
String str="hello world world";
String [] result=str.split(" ");
System.out.println(Arrays.toString(result));
}
}
public class Func {
public static void main(String[] args) {
//ip地址
String a="192.168.0.1";
//正则表达式里面有很多特殊符号代表特殊含义,'.'就是其中一个
//为了解决这个问题,需要使用“正则表达式”中的转义字符
//正则表达式见到.当做特殊符号来表示
//见到/.当做.来使用
//java中\也当做转义字符,因此应该写成两个\\.
//这里的\\一个可以理解为Java的转义字符,一个可以理解为.的转义
String [] result=a.split("\\.");
System.out.println(Arrays.toString(result));
}
}
3.5字符串的截取
public class Func {
public static void main(String[] args) {
String a="hello world java";
//[beg,end)
System.out.println(a.substring(6,11));
System.out.println(a.substring(6));
}
}
3.6其他方法的使用
public class Func {
public static void main(String[] args) {
//trim 去掉字符串左右的空白符(空格,换行,回车,制表符,翻页符,垂直制表符...)
String a=" hello world \n";
System.out.println(a);
System.out.println("["+a.trim()+"]");
}
}
//toUpperCase字母全部转换为大写字母
//toLowerCase字母全都转换为小写字母
public class Func {
public static void main(String[] args) {
String a="Hello";
System.out.println(a.toUpperCase());
System.out.println(a.toLowerCase());
}
}
//intern字符串放入常量池中
//isEmpty判断是不是空字符串
String a="";//空字符串
String b=null;//空引用
需要注意的是:上述这些方法都是创建新的对象,而不是修改原来的字符串。
4.StringBuilder和StringBuffer
String是不可变对象,不能直接修改内容。如果我们需要使用可变版本的String,就需要使用StringBuffer或者StringBuilder。
StringBuffer和StringBuilder用法基本一致。以StringBuilder为例:
public class Func {
public static void main(String[] args) {
//1.append:使用append能够把字符串的内容进行追加,相当于String+=
//String的+=会产生新的String对象,如果在循环中使用
//就会显得很低效
String str="hello";
for(int i=0;i<100;i++){
str+=i;
}
StringBuilder stringBuilder=new StringBuilder("hello");
for(int i=0;i<100;i++){
//因为StringBuilder是可变对象
//所以在使用append就是直接把参数拼接到原来内存的末端了
//如果拼接的内容太多,超出内存范围,StringBuilder就会扩容
stringBuilder.append(i);
}
StringBuilder stringBuilder=new StringBuilder("hello");
//直接修改本身
stringBuilder.reverse();
System.out.println(stringBuilder.toString());
//删除元素
stringBuilder.delete(2,4);
System.out.println(stringBuilder.toString());
StringBuilder stringBuilder=new StringBuilder("hello");
stringBuilder.insert(2,"world");
System.out.println(stringBuilder.toString());
}
}
StringBuilder和StringBuilder的核心区别:
StringBuilder是线程不安全的,StringBuffer是线程安全的。