String类的构造方法很多,常见的有以下的三种:
//三种常用的构造方法
public class Test {public static void main(String[] args) {//使用常量串构造String s1 = "hello world!";//直接new String对象构造String s2 = new String("hello world!");//使用字符串数组进行构造char[] array = {'h', 'e', 'l', 'l', 'o'};String s3 = new String(array);}
}
【注意】
System.out.printlen(“hello”.lenth());
//这个 hello 也是String类型
我们如何比较字符串的大小呢?Java中共提供了四种方法:
//==比较是否引用同一个对象
public class Test {public static void main(String[] args) {String s1 = new String("nihao");String s2 = new String("nihao");String s3 = s1;System.out.println(s1 == s2);System.out.println(s1 == s3);}
}
这里s1和s2引用的是不同的对象,使用”==“比较时候,地址不同,所以不相等。而s1和s3是引用的同一对象,所以相同。
2. boolean equals(Object anObject)方法:按照英文字母顺序查找
String类中重写了父类 Object中equals方法,Object中equals默认按照 == 比较,String重写equals方法后,按照如下规则比较:
比如”hello"与“hello world”比较,先比较长度,长度不相等,返回false ; 长度如果相等逐个字母进行比较,不相同返回false;全部相同,返回true
3. int compareTo(String s)方法按照英文顺序比较
与equals不同的是,equals返回值是boolean类型,而compareTo返回的是int类型,比较的方法如下:
// int compareTo(String s) 方法
public class Test {public static void main(String[] args) {String s1 = "xiaoming";String s2 = "xiaohong";String s3 = "miaomiao";String s4 = "xiao";System.out.println(s1.compareTo(s2));//前几个字母相等,返回m-h的值System.out.println(s1.compareTo(s3));//返回x-m的值System.out.println(s1.compareTo(s4));//前几个字母相等,返回字母数量的差值}
}
//int compareTolgnoreCase(String str)方法
public class Test {public static void main(String[] args) {String s1 = "XIAOMING";String s2 = "xiaoming";String s3 = "mingming";System.out.println(s1.compareToIgnoreCase(s2));//输出为0System.out.println(s1.compareToIgnoreCase(s3));//首字母大小不一样,返回首字母大小的差值}
}
字符串查找也是字符串中常见的操作,String类提供的常见查找方法:
方法 | 功能 |
---|---|
char chaAt(int index) | 返回值index位置上字符,如果index为负数或者越界,将抛出IndexOutOfBountsException异常 |
int indexof(int ch) | 返回ch第一次出现出现的位置,没有返回-1 |
int indexOf(int ch, int fromIndex) | 冲fromIndex位置开始找ch第一次出现的位置,没有返回-1 |
int indexof(Sting str) | 返回str第一次出现的位置,没有返回-1 |
int indexof(Sting str, int fromIndex) | 从fromIndex位置考试找str第一次出现的位置,没有返回-1 |
int lastIndexof(int ch) | 从后往前找,返回ch第一次出现的位置,没有返回-1 |
int lastIndexof(int ch, int fromIndex) | 从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1 |
int lastIndexOf(String str) | 从后往前,返回str第一次出现的位置,没有返回-1 |
int lastIndexOf(String str, int fromIndex) | 从fromIndex位置开始找,从后往前找返回第一个str的位置,没有返回-1 |
//字符、字符串查找相关函数
public class Test {public static void main(String[] args) {String s = "abcdcba";System.out.println(s.charAt(0));//找到第一个字符,查看是什么System.out.println(s.indexOf('c'));//从前往后找第一个cSystem.out.println(s.indexOf('c', 3));//从第四个字母开始找,出现c的位置System.out.println(s.indexOf("cba"));//从前往后找找第一个cbaSystem.out.println(s.lastIndexOf('c'));//从后向前找第一个cSystem.out.println(s.lastIndexOf('c', 3));//从第四个位置开始,找第一个cSystem.out.println(s.lastIndexOf("cb"));//从后向前,找第一次出现cb的位置System.out.println(s.lastIndexOf("cb", 5));//从第五个位置从后向前找cb}
}
//数值和字符串转化
class Student {String s;int age;public Student(String s, int age) {this.s = s;this.age = age;}
}public class Test {public static void main(String[] args) {String s1 = String.valueOf(123);String s2 = String.valueOf(3.1415926);String s3 = String.valueOf(true);String s4 = String.valueOf(new Student("xiaoming", 18));System.out.println(s1);System.out.println(s2);System.out.println(s3);System.out.println(s4);//字符串转化为数字int a = Integer.parseInt(s1);double b = Double.parseDouble(s2);System.out.println(a);System.out.println(b);}
}
//大小写转化
public class Test {public static void main(String[] args) {String s1 = "hello WORLD!";String s2 = "nihao 2022";System.out.println(s1.toLowerCase());System.out.println(s1.toUpperCase());System.out.println(s2.toUpperCase());}
}
//字符串转数组
public class Test {public static void main(String[] args) {String s = "hello";char[] ch = s.toCharArray();for (int i = 0; i < ch.length; i++) {System.out.print(ch[i]);}System.out.println();String s2 = new String(s);System.out.println(s2);}
}
//格式化
public class Test {public static void main(String[] args) {String s = String.format("%d-%d-%d",2022,9,14);System.out.println(s);}
}
使用一个新的字符串替换掉已有的字符串数据
String replaceAll(String regex, String replacement)//替换所有的
String replaceFirst(String regex, String replacement)//替换第一个 regex
//字符串替代
public class Test {public static void main(String[] args) {String s = "hello world!";System.out.println(s.replaceAll("l", "-"));System.out.println(s.replaceFirst("l","-"));}
}
这里我们要注意,由于字符串是不可变对象,替换并不修改当前的字符串,而是创建一个新的字符串
将一个完整的字符串按照指定的分隔符进行查分成若干个子字符串。
String[] split(String regex) //将字符串根据regex进行全部拆分
String[] split(String regex, int limit);//将字符串以指定的格式进行差分
//字符串拆分
public class Test {public static void main(String[] args) {String str = "hello world, hello baobo!";String[] result = str.split("ll");for (String s: result) {System.out.println(s);}System.out.println("===========================");String[] result1 = str.split(" ", 2);//2代表分成两端for (String s: result1) {System.out.println(s);}}
}
【注意】有些特殊字符作为分隔符时,需要加上转义字符
//特殊字符作为分割字符
public class Test {public static void main(String[] args) {String s1 = "zhansan*age=18";String[] s2 = s1.split("\\*");System.out.println(s2[0]);System.out.println(s2[1]);System.out.println("=============================");String s3 = "d\\xuexi\\wuli";String[] s4 = s3.split("\\\\");System.out.println(s4[0]);System.out.println(s4[1]);System.out.println(s4[2]);System.out.println("=============================");String[] s5 = s1.split("\\*|=");System.out.println(s5[0]);System.out.println(s5[1]);System.out.println(s5[2]);}
}
从一个完整的字符串中截取部分内容,
String substring(int beginIndex);//从某一下标处开始截取
String substring(int beginIndex,int endIndex);//从 begin下标处截取到end下标出
//字符串截取
public class Test {public static void main(String[] args) {String s1 = "nihao xiaoming";System.out.println(s1.substring(6));System.out.println(s1.substring(1,6));}
}
【注意事项】
方法 | 功能 |
---|---|
String trim() | 去掉字符串中的左右空格,保留中间空格。 |
String toUpperCase() | 字符串大写 |
String toLowerCase() | 字符串转小写 |
public class Test {public static void main(String[] args) {String s = " Hello world! ";System.out.println(s.trim());//trim去掉字符串两边的空格System.out.println(s.toUpperCase());//将字符按转化为大写System.out.println(s.toLowerCase());//将字串转化为小写}
}
String是一种不可变独享,字符串中的内容是不能改变的。字符串不可改变的愿意:
为什么String要被设计成不可改变的呢?(选学)
- 方便实现字符串对象池。如果String可变,那么对象池就可能需要考虑写时拷贝的问题;
- 不可变对象的线程是安全的;
- 不可变对象更方便缓存hash code,作为key时可以更高效的保存到HashMap中。
【注意】尽量避免直接对String类型对象进行修改,因为String类是不可能修改的,所有的修改都会创建新对象,效率非常低
//字符串避免频繁修改
public class Test {public static void main(String[] args) {String s = "hello";s += "world";System.out.println(s);}
}
像这种方式,其实中间回创建很多的临时对象,效率很低
由于String的不可更改特性,为了方便字符串的修改,Java中提供了StringBuider和StringBuffer类。
//StringBuilder的一些方法
public class Test {public static void main(String[] args) {StringBuilder s1 = new StringBuilder("Hello");StringBuilder s2 = s1;s1.append(' ');//追加字符s1.append("world");//追加字符串s1.append(123);//追加数字System.out.println(s1);//在原本的基础上进行追加System.out.println(s1 == s2);//trueSystem.out.println(s1.charAt(0));//获取0下标的字符System.out.println(s1.length());//获取长度System.out.println(s1.capacity());//获取底层数组总大小s1.setCharAt(0,'h');//改变0位置的字符s1.insert(0, "nihao!");//在0下标前面,追加字符串System.out.println(s1);System.out.println(s1.indexOf("hello"));//获取第一次hello出现的位置System.out.println(s1.lastIndexOf("hello"));//获取从前往后数第一次出现hello的位置s1.deleteCharAt(0);//删除首字符s1.delete(0,5);//删除【0,6)范围内的字符System.out.println(s1.substring(0, 6));//截取【0,6)范围内的字符s1.reverse();//字符串逆转System.out.println(s1.toString());//以字符串形式输出}
}
StringBuilder的内容可以被改变(在原来的内容上进行修改);
【注意】String 和 StringBuilder类不能直接转化,如果要相互转化要满足如下原则:
public class Test {public static void main(String[] args) {String str1 = new String("ab");//创建多少String对象 2String str2 = new String("a") + new String("b");//创建多少String对象 4}
}