Java String函数的使用
文章目录

String
- String 的构造方法
str本来是字符串常量的引用,应该打印地址,但是编译器重写了toString方法,所以打印hello

publicclasstest{publicstaticvoidmain(String[] args){// 用常量字符串构造 String s1 ="hello"; System.out.println(s1);// 用字符数组构造char[] array =newchar[]{'a','b','c'}; String s2 =newString(array); System.out.println(s2);char[] array1 =newchar[]{'a','b','c'}; String s4 =newString(array,0,2);// 从0位置后拿2个字符 System.out.println(s4);// 直接newSting对象构造 String s3 =newString("hello"); System.out.println(s3);}}- String对象在内存中的情况


4. 空指针异常和空字符串,isEmpty()判断是否为空字符串

字符串比较
- s1 == s2 比较地址
- s1.equals(s2)比较是否相等,返回true或者false
- s1.compareTo(s2)比较大小
- s1.compareToIgonreCase(s2)忽略大小写比较
publicclass test {publicstaticvoidmain(String[] args){String s1 =newString("hello");String s2 =newString("Hello");System.out.println(s1 == s2);// 不等于,s1和s2表示对象的引用都存的是地址System.out.println(s1.equals(s2));System.out.println(s1.compareTo(s2));// s1 大于 s2 返回正数// s1 小于 s2 返回负数// s1 等于 s2 返回0System.out.println(s1.compareToIgnoreCase(s2));// 忽略大小写比较}}字符串查找
- char charAt(int index),返回数组中下标对应的字符
publicstaticvoidmain(String[] args){String s1 =newString("hello");char ch = s1.charAt(1);System.out.println(ch);// e}- int indexOf(char ch),返回第一次出现ch字符的下标
String s2 =newString("hello");int index = s1.indexOf('l');System.out.println(index);// 2- int indexOf(char ch,int k),k表示下标,从指定位置开始查找
String s2 =newString("hello");int index = s1.indexOf('l',3);System.out.println(index);// 3- int indexOf(String s),可以查找子串在主串中出现的位置,如果没有找到返回-1
String s3 ="ababcdeabcf";int index = s3.indexOf("abc");System.out.println(index);// 2int index1 = s3.indexOf("abc",3);System.out.println(index1);// 7 - int lastIndexOf(String s),倒着往前找,返回第一个找到的下标
String s3 ="ababcabcd";int index = s3.lastIndexOf("abc");System.out.println(index);// 5int index1 = s3.lastIndexOf("abc",4);System.out.println(index1);// 2// 从4下标位置倒着往前找转化
- 数字和字符串之间的转化
String s2 =String.valueOf(newStudent("zhangsan",20));System.out.println(s2);String s3 =String.valueOf(123);System.out.println(s3);String s4 =String.valueOf(123.34);System.out.println(s4);String s5 =String.valueOf(true);System.out.println(s5);// true
2. 字符串转数字
int a =Integer.parseInt("190");System.out.println(a);int b =Integer.parseInt("19.9");// 错误,给的要是整数的字符串
- 大小写转化
小写转大写:toUpperCase
转变为大写不是在原有字符串的基础上转换,而是转变为大写是一个新的对象,不会改变原有的字符串
String s ="hello";String ret = s.toUpperCase();System.out.println(ret);大写转小写
String s ="HEllo";String ret = s.toLowerCase();System.out.println(ret);// hello- 字符串转为数组
String s ="hello";char[] ret = s.toCharArray();System.out.println(Arrays.toString(ret));// [h,e,l,l,o]数组转为字符串
char[] ret ={'a','b','c'};String s =newString(ret);System.out.println(s);// abc- 格式转换
String s =String.format("%d-%d-%d",2019,9,20);System.out.println(s);// 2019-9-20字符串替换
- 替换字符串

- 替换单个字符

3. 替换第一个ab

4. 把所有的ab都替换为123

字符串拆分
- s.split()以这里面的字符串为标准分割
publicstaticvoidmain(String[] args){String s ="hello world k";String[] array = s.split(" ");System.out.println(Arrays.toString(array));// [hello,world,k]String s1 ="hello world k";String[] array1 = s.split(" ",2);// 以空格分割最多分成两组System.out.println(Arrays.toString(array1));// [hello, world k]}- 特殊的情况,使用转义字符




多次分割

字符串截取(常用)
- substring(a,b) [a,b) a和b均为下标
String str ="hello";String ret = str.substring(0,3);// hel- 给一个参数,会把后面的全不截取
String str ="hello";String ret = str.substring(0);// hello- trim(),去掉字符串的左右空格,中间的空格不可去掉

字符串的不可变性
- 字符串中的value[] 数组是被private修饰的,也没有提供get方法,在类外是无法拿到的,就无法修改该数组了
- 而被final修饰的,只是表明它是常量了,它的引用只能指向一个对象,不能被改变成指向别的对象
- 被final修饰的不能被继承
finalint[] array ={1,2,3};// array = new int[]{1,2};// 不能改变array的指向了 array[0]=2;// 可被修改