Java中String类的常用方法以及String,StringBuffer,StringBuilder的区别

一.认识String类

String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。

二.String类对象的创建

1.String str = "Hello";

2.String str = new String("Hello");

三.String类的常用方法

1.int length():返回字符串的长度

2.char charAt(int index):返回指定索引处的字符

3.String toLowerCase():将字符串中的所有字符转换为小写

4.String toUpperCase():将字符串中的所有字符转换为大写

5.String trim():返回字符串的副本,去掉前导空白和尾部空白,中间的空白不会被去掉

6.boolean equals(Object obj):比较字符串的内容是否相同

7.boolean equalsIgnoreCase(String anotherString):忽略大小写,比较字符串的内容是否相同String concat(String str):将指定字符串连接到此字符串的结尾,等价于用“+”

8.int compareTo(String anotherString):比较两个字符串的大小

9.String substring(int beginIndex):返回从beginIndex到末尾的子字符串

10.String substring(int beginIndex, int endIndex):返回从beginIndex到endIndex前一位的子字符串,不包括endIndex

11.boolean endsWith(String suffix): 判断字符串是否以指定的后缀结束

12.boolean startsWith(String prefix):判断字符串是否以指定的前缀开始

13.boolean startsWith(String prefix, int toffset):判断字符串在指定索引开始的子字符串是否以指定前缀开始

14.boolean contains(CharSequence s):判断当前字符串中是否包含指定的字符串

15.int indexOf(String str):返回指定子字符串在当前字符串中第一次出现处的索引

16.int indexOf(String str, int fromIndex):返回从指定的索引后,指定子字符串在当前字符串中第一次出现处的索引

17.int lastIndexOf(String str):返回指定子字符串在当前字符串中最后一次出现处的索引

18.int lastIndexOf(String str, int fromIndex):返回从指定的索引后,指定子字符串在当前字符串中最后一次出现处的索引

19.String replace(char oldChar, char newChar):替换当前字符串中指定的子字符串

20.String[] split(String regex):根据指定的符号拆分当前字符串,然后返回一个String数组

代码示例:

public class Main {
    public static void main(String[] args) {
        //方法调用:1.调用方法 对象.方法名()2.传入参数;3.接受方法的返回值
        String s1="asdfg";
        char c=s1.charAt(2);//获取字符中指定索引位置的字符
        System.out.println(c);
        System.out.println("-----------");
        String s2=s1.concat("qwe");//字符串的拼接
        System.out.println(s2);
        System.out.println("-----------");
        boolean b1=s1.contains("qwe");//判断字符串中是否存在某个字符
        boolean b2=s2.contains("qwe");
        System.out.println(b1);
        System.out.println(b2);
        System.out.println("-----------");
        String s3="yueyougong.wwe";
        boolean b3=s3.endsWith(".wwe");//判断文件名的后缀
        System.out.println(b3);
        System.out.println("-----------");
        
        byte[] bytes1={1,2,3,4,5,6};
        String s4=new String(bytes1);//把字符串按照ASCII解析成byte数组
        System.out.println("s4:"+s4);
        System.out.println("-----------");
        byte[] bytes2={1,2,3,4,5,6};
        String s5=new String(bytes2,2,2);//对bytes2数组从索引2开始解析2个长度
        System.out.println(s5);
        System.out.println("-----------");
        char[] char1={'a','b','c','d','e','f'};
        String s6=new String(char1);
        System.out.println("s6:"+s6);
        System.out.println("-----------");
        char[] char2={'a','b','c','d','e','f'};
        String s7=new String(char1,2,2);
        System.out.println("s7:"+s7);
        System.out.println("-----------");


    }
}

四.String,StringBuffer,StringBuilder的区别

1.String、StringBuffer、StringBuffer都是final类,不允许被继承;

2.String声明的对象进行内容修改会产生一个新的对象,而StringBuffer、StringBuilder则是对自身进行修改,不会产生新的对象;

3.运行速度:StringBuilder > StringBuffer > String

4.线程安全:StringBuilder < StringBuffer

:String和StringBuilder最大的区别在于 String 的内容无法修改,而 StringBuilder 的内容可以修改。频繁修改字符串的情况考虑使用StringBuilder。

猜你喜欢

转载自blog.csdn.net/AMYCX/article/details/128192279