一、字符串

一、操纵字符串的方法

1.charAt(int index)方法

  获取指定位置的字符

package property;
public class TestString{
    public static void main(String[] args){
        String str="abcdefgh";
        char ch=str.charAt(3);
        System.out.println(ch);//d
    }
}
View Code

2.toCharArray()方法

  获取对应的字符数组

package property;
public class TestString{
    public static void main(String[] args){
        String str="abcdefgh";
        char[] ch=str.toCharArray();//获取对应的字符数组
        System.out.println(str.length()==cs.length);//true,数组有length属性,String类型有length()方法
    }
}
View Code

3.subString()方法

  截取子字符串

package property;
public class TestString{
    public static void main(String[] args){
        String str="abcdefgh";
        String subStr1=str.subString(3);//截取从第3个开始的字符串
        System.out.println(subStr1);//defgh
        String subStr2=str.subString(3,5);//截取从第3个开始到第5-1=4位置的字符串也就是在区间[3,5)的
        System.out.println(subStr2);//de
    }
}
View Code

4.spilt()方法

  根据分隔符进行分隔

package property;
public class TestString{
    public static void main(String[] args){
        String str="abcde_fgh";
        String subStr[]=str.spilt("_");//根据"_",得到两个部分,一部分为abcde,一部分为fgh
        for(String sub:substr){
            System.out.println(sub);
        }
    }
}
View Code

5.trim()

  去掉首尾空格

package property;
public class TestString{
    public static void main(String[] args){
        String str="     abcde_fgh  ";
        System.out.println(str);
        //去掉首尾空格
        System.out.println(str.trim());
        }
    }
}
View Code

6.toLowerCase/toUpperCase

  变小写/变大写

package property;
public class TestString{
    public static void main(String[] args){
        String str="SabXcdefgh";
        //全部变小写
        System.out.println(str.toLowerCase());
        //全部变大写
        System.out.println(str.toUpperCase());
        }
    }
}
View Code

7.indexOf()方法/contains()方法

  判断字符或者子字符串出现的位置

package property;
public class TestString{
    public static void main(String[] args){
        String str="SabXcdefghcyu";
        System.out.println(str.index('c'));//字符c第一次出现的位置
        System.out.println(str.lastIndexOf('c'));//字符c最后一次出现的位置
        System.out.println(str.indexOf('c',5));//从位置5开始,字符c第一次出现的位置
        System.out.println(str.contains("fgh"));//是否包含字符串fgh
        }
    }
}
View Code

8.replaceAll()/replaceFirst()

  替换所有/只替换第一个

package property;
public class TestString{
    public static void main(String[] args){
        String str="盖+伦,在进行了连续8次击杀后,获得了超神 的称号";
        String temp=str.replaceAll("击杀","被击杀")//将所有的击杀,替换为被击杀
        temp=temp.replaceAll("超神","超鬼");//将所有的超神,替换为超鬼
        System.out.println(temp);
        temp=str.replaceFirst("+","");//只替换第一个
        System.out.println(temp);
        }
    }
}
View Code

9.比较是否同一个对象

package character;
 
public class TestString {
 
    public static void main(String[] args) {
 
        String str1 = "the light";
         
        String str2 = new String(str1);
         
        //==用于判断是否是同一个字符串对象
        System.out.println( str1  ==  str2);
         
    }
 
}
View Code

10.StringBuffer

  是可变长的字符串

append/delete/insert/reverse:追加/删除/插入/反转

package property;
public class TestString{
    public static void main(String[] args){
        String str="abcdefgh";
        StringBuffer sb=new StringBuffer(str);//根据str创建一个StringBuffer对象
        sb.append("Hello World");//在最后面追加
        System.out.println(sb);
        sb.delete(4,10);//删除4-10之间的字符串
        System.out.println(sb);
        sb.insert(4,"love");//在4这个位置插入love
        System.out.println(sb);
        sb.reverse();//反转
        System.out.println(sb);
    }
}
View Code

11.为什么StringBuffer可变长?

和String内部是一个字符数组一样,StringBuffer也维护了一个字符数组,但是这个字符数组,留有多余的长度。

String和StringBuffer的性能区别?

使用String的"+"方式连接字符串,消耗时间比,StringBuffer追加连接的时间长

原文地址:https://www.cnblogs.com/drq1/p/8466954.html