常用方法
字符串构造
String类提供的构造方法方式非常多,常见三种
public static void main(String[] args){
//使用常量串构造
String s1="hello bit";
String.out.println(s1);
//直接newString对象
String s2=new String("hello bit");
System.out.println(s1);
//使用字符数组进行构造
char[] array={'h','e','l','l','o','b','i','t'};
System.out.println(s1);
}
【注意】
1.String是引用类型,内部并不储存字符串本身,在String类的实现源码中,String类实例变量如下:
public final class String
implements java.io.Serializable,Comparable<String>,CharSequence{
private final char vaule[];
private int hash;//Default to 0
public static void main(String[] args){
//s1和s2引用的是不同对象 s1和s3引用的是同一对象
String s1=new String("hello");
String s2=new String("world");
String s3=s1;
System.out.println(s1.length());//获取字符串长度---输出5
System.out.println(s1.isEmpty());//如果字符串长度为0,返回true,否则返回flase
}
String对象的比较
1.==比较是否引用同一个对象
注意:对于内置类型,= =比较的是变量中的值;对于引用类型= =比较的是引用中的地址。
public static void main(String[] args){
int a=10;
int b=20;
int c=10;
//对于基本变量,==比较两个变量中存储的值是否相同
System.out.println(a==b);//flase
System.out.println(a==c);//true
//对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
Srting s1=new String("hello");
String s2=new String("hello");
String s3=new String("world");
String s4=s1;
System.out.println(s1==s2);//false
System.out.println(s2==s3);//flase
System.out.println(s1==s4);//true
2.boolean equals(Object anObject)方法:按照字典序比较
字典序:字符串大小的顺序
String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写方法后,按照如下规则进行比较:s1.equals(s2)
public boolean equals(Object anObject){
//1.先测试this和anObject是否为同一个对象比较,如果是返回true
if(this==anObject){
return true;
}
//2.检测anObject是否为String类型的对象,如果是继续比较,否则返回false
if(anObject instanceof String){
//将anObject向下转性为String类型对象
String anotherString=(String)anObject;
int n=vaule.length;
//3.this和anObject两个字符串的长度是否相同,是继续比较,否则返回flase
if(n==anotherString.value.length){
char v1[]=value;
char v2[]=anotherString.value;
int i=0;
//4.按照字典排序,从前往后逐个字符进行比较
while(n--!=0){
if(v1[i]!=v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
public static void main(String[] args){
String s1=new String("hello");
String s2=new String("hello");
String s3=new String("Hello");
//s1,s2,s3引用的是三个不同对象,因此==比较结果全部为false
System.ot.println(s1==s3);//false
System.out.println(s1==s3);//false
//equals比较:String对象中的逐个字符,虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//false
}
3.int compara To(String s)方法:按照字典序进行比较
与equals不同的是,equals返回的是boolean类型,而comparaTo返回的是int类型。
1.先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2.如果前K个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
public staic void main(String[] args){
String s1=new String("abc");
String s2=new String("ac");
String s3=new String("abc");
String s4=new String("abcdef");
System.out.println(s1.comparaTo(s2));//不同输出字符差值-1
System.out.println(s1.comparaTo(s3));//相同输出0
System.out.println(s1.comparaTo(s4));//前k个字符完全相同,输出长度差值-3
}
4.int comparaToIgnoreCase(String str)方法:与comparaTo方式相同,但是忽略大小写比较
public static void main(String[] args){
String s1=new String("abc");
String s2=new String("ac");
String s3=new String("ABc");
Strinf s4=new String("abcdef");
System.out.println(s1.comparaToIgnoreCase(s2));//不同输出字符差值-1
字符串查找
字符串查找也是字符串中非常常见的操作,String类提供的常用查找的方法:
方法 | 功能 |
---|---|
char charAt(int index) | 返回index位置上字符,如果idex为负数或者越界 |
int indexOf(int ch) | 返回ch第一次出现的位置,没有返回-1 |
int index Of(int ch,int formIndex) | 从fromIndex位置开始找ch第一次出现的位置,没有返回-1 |
int indexOf(String str) | 返回str第一次出现的位置,没有返回-1 |
pubilc static void main(String[] args){
String s="aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); //'b'
System.out.println(s.indexOf('c')); //6
System.out.println(s.indexOf('c',10));//15
System.out.println(s.indexOf("bbb"))://3
System.out.println(s.indexOf("bbb",10));//12
System.out.println(s.lastIndexOf('c'));//17
System.out.println(s.lastIndexOf("bbb"));//12
System.out.println(s.lastIndexOf("bbb",10);//3
转换
1.数值和字符串转换
public static void main(String[] args){
//数字转字符
String s1=String.valueOf(1234);
String s2=String.valueOf(12.34);
String s3=String.vauleOf(true);
Srting s4=String.valueOf(new Student("Hanmeimei",18));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
//字符串转数字
int data1=Integer.parselent("1234");
double data2=Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
System.out.println(data3);
System.out.println(data4);
}
2.大小写转换
public static void main(Strinf[] args){
String s1="hello";
String s2="HELLO";
//小写转大写
System.out.println(s1.toUpperCase());
//大写转小写
System.out.println(s2.toLowerCase());
}
3.字符串转数组
public static void main(String[] args){
String s="ello":
//字符串转数组
char[] ch=s.tpCharArray();
for(int i=0;i<ch.length;i++){
System.out.println(ch[i]);
}
System.out.println();
//数组转字符串
String s2=new String(CH);
System.out.println(s2);
}
4.格式化
public static void main(String[] args){
String s=String.format("%d-%d-%d",20200);
System.out.println(s);
}
字符串替换
使用一个指定的新的字符串替换掉已有的字符串数据,可用方法:
方法 | 功能 |
---|---|
String replaceAll(String regex,String replacement) | 替换所有的指定功能 |
String replaceFirst(String regex,String replacement) | 替换首个内容 |
代码示例
String str="helloworld";
System.out.println(str.replaceAll("|","_"));
System.out.println(str.replaceFirst("|","_"));
注意事项:由于字符串不可变对象,替换不修改当前字符串,而是产生一个新的字符串。
字符串的拆分
可以将一个完整的字符串按照指定的分隔符划分成若干个子字符串。
方法 | 功能 |
---|---|
String[] split(String regex) | 将字符串全部拆分 |
String[] split(String regex,int limit) | 将字符串以指定的格式,拆分为limit组 |
代码示例:实现字符串的拆分处理
String str="hello world bit ";
String[] result=str.split("");//按照空格拆分
for(String s:result){
System.out.println(s);
}
代码示例:字符串的部分拆分
String str="hello world hello bit";
String[] result=str.split(" ",2);
for(String:result){
System.out.println(s);
}
拆分是特别常用的操作,一定要重视掌握,另外有些特殊字符作为分隔符可能无法正确切分,需要加上转义。
注意事项:
1.字符“|”,“*”,“+”,“-“都要加上转义字符,1前面加上”\”.
2.而如果是”“,那么就要写成”\\"
3.如果一个字符串中有多个分隔符,可以用“|”作为连字符。
代码示例:多次拆分
String str="name=zhangsan&age=18";
String[] result=str.split("&");
for(int i=0;i<result.length;i++){
String[] temp =result[i].split("=");
System.out.println(temp[0]+"="+temp[1]);
}
字符串的截取
从一个完整的字符串之中截取出部分内容。
方法 | 功能 |
---|---|
String substring(int beginIndex) | 从指定索引截取到结尾 |
String substring(int beginindex,int endIndex) | 截取部分内容 |
代码示例:观察字符串截取
String str="helloworld";
System.out.println(str.substring(5));
System.out.prontln(str.substring(0,5));
注意事项:
1.索引从0开始
2.注意前闭后开区间的写法,Substring(0,5)表示包含0号下标的字符,不包含5号下标
其他操作方法
方法 | 功能 |
---|---|
String trim() | 去掉字符串中的左右空格,保留中间空格 |
String toUpperCase() | 字符串转大写 |
Srting toLowerCase() | 字符串转小写 |
代码实例:观察trim()使用
String str="hello world";
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
trim会去掉字符串开头和结尾的空白字符(空格,换行,制表符等)。
字符串的不可变性
String是一种不可变对象,字符串中的内容是不可改变的。
1.String类在设计时就是不可改变的
String类中的字符实际保存在内部维护的value字符数组中,
1.String类被String修饰,表明value自身的值不能改变,既不能引用其他字符数组,但是引用空间中的内容可以修改。
2.value被修饰被final修饰,表明value自身的值不能改变,即不能引用其他字符数组,但是其引用空间中的内容可以修改
2.所涉及到可能修改字符串内容的操作是创建一个新对象,改变的是新对象。
比如:replace方法
public String replace(char oldChar,char newChar){
if(oldChar !=newChar){
int len =value.length;
int i=-1;
char[] val=value;
while (++i<len){
if(val[i] ==oldChar){
break;
}
}
if(i<len){
char buf[]=new char[len];
for(int j=0;j<i;j++){
buf[j]=val[j];
}
while (i<len){
char c=val[i];
buf[i]=(c==oldChar)?newChar:c;
i++;
}
return new String(buf,true);
}
final修饰类表明该类不想被继承,final修饰引用类型表明该引用类不能引用其他对象,但是其引用对象中的内容是可以修改的。
public static void main(String[] args){
final int array[]={1,2,3,4};
array[0]=100;
System.out.println(Arrays.toString(array));
//array=new int[]{4,5,6};
}
字符串修改
注意:尽量避免直接对String类型对象进行修改,因为String类是不能修改的,所有修改都会创建新对象,效率低下。
public static void main(String[] args){
String s="hell";
s+="world";
System.out.println(s);//输出:hello world
}
但是这种方式不推荐使用,因为效率非常低,中间创建好多对象。