Java_常用类03_String类

一、    java.lang.String类:JAVA中不可变的字符序列。

i.    每次对String的修改都会产生新的String对象。工作时如果需要大量修改字符串时,不建议使用String类。

ii.    String是final的类。

iii.    String重写了Object类的equals(),用于判断字符串的内容是否相等(区分大小写)。

iv.    String的方法不会改变自身的内容。

二、    java.lang.StringBuffer类与java.lang.StringBuilder类

i.    JAVA中可变的字符序列。

ii.    StringBuffer是线程安全的(线程同步的)

iii.    StringBuilder是线程不安全的(线程不同步的)

iv.    每次对StringBuffer与StringBuilder的修改不会产生新的对象,工作中如果需要大量的修改字符串时,建议使用StringBuffer或StringBuilder

v.    StringBuffer与StringBuilder的方法会改变自身的内容。

vi.    StringBuffer与StringBuilder没有重写Object类中的equals(),所以不能使用equals()比较StringBuffer与StringBuilder的内容是否相等。

 

String类:代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。

(1)    多个字符组成的一串数据。其实它可以和字符数组进行相互转换。

 

(2)    构造方法:

        A:public String():空构造

        B:public String(byte[] bytes):把字节数组转换成字符串

        C:public String(byte[] bytes,int offset,int length)把字节数组的指定部分转换成字符串

        D:public String(char[] value):把字符数组转换成字符串

        E:public String(char[] value,int offset,int count):把字符数组的指定部分转换成字符串

        F:public String(String original)字符串常量转换成字符串

下面的这一个虽然不是构造方法,但是结果也是一个字符串对象

G:String s = "hello"; //字符串常量

            

(3)    字符串的特点

        A: 字符串是常量;它们的值在创建之后不能更改(字符串一旦被赋值,就不能改变。)

            注意:这里指的是字符串的内容不能改变,而不是引用不能改变

        

        B:字面值作为字符串对象和通过构造方法创建对象的不同

            String s = new String("hello");和String s = "hello"的区别?

        

(4)    字符串的面试题(看程序写结果)

        A:==和equals()

            String s1 = new String("hello");

            String s2 = new String("hello");

            System.out.println(s1 == s2);// false

            System.out.println(s1.equals(s2));// true

 

            String s3 = new String("hello");

            String s4 = "hello";

            System.out.println(s3 == s4);// false

            System.out.println(s3.equals(s4));// true

 

            String s5 = "hello";

            String s6 = "hello";

            System.out.println(s5 == s6);// true

            System.out.println(s5.equals(s6));// true

        B:字符串的拼接

**字符串如果是变量相加,先开空间,再拼接。

**字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。

//通过反编译工具查看

            String s1 = "hello";

            String s2 = "world";

            String s3 = "helloworld";

            System.out.println(s3 == s1 + s2);// false 2个字符串做拼接,因为它是变量,所以先开空间再拼接。

            System.out.println(s3.equals((s1 + s2)));// true

            System.out.println(s3 == "hello" + "world");// true 因为它是常量,先加后找,没有就创建。

            System.out.println(s3.equals("hello" + "world"));// true

(5)    字符串的功能(自己补齐方法中文意思)

        A:判断功能

            boolean equals(Object obj):比较字符串的内容是否相同,区分大小写

            boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写

            boolean contains(String str):判断大串中是否包含小串(连一起的)

            boolean startsWith(String str):判断字符串是否以某个指定的字符串开头

            boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾

            boolean isEmpty():判断字符串是否为空

                

注意:

字符串内容为空和字符串对象为空:

String s = "";

String s = null;

        B:获取功能

            int length():获取字符串的长度

            char charAt(int index):获取指定索引位置的字符

            int indexOf(int ch):返回指定 字符 在此字符串中第一次出现处的索引,不存在返回-1。

为什么这里是int类型,而不是char类型?

原因是:'a'和97其实都可以代表'a'

            int indexOf(String str):返回指定 字符串 在此字符串中第一次出现处的索引,不存在返回-1。

            int indexOf(int ch,int fromIndex):返回指定 字符 在此字符串中从指定索引位置后第一次出现处的索引,不存在返回-1。

            int indexOf(String str,int fromIndex):返回指定 字符串 在此字符串中从指定索引位置后第一次出现处的索引,不存在返回-1。

            String substring(int beginIndex):返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。

            String substring(int beginIndex, int endIndex):返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,

直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。

            

        C:转换功能

            byte[] getBytes():把字符串转换为字节数组。输出的是对应的ASCII码值,可强转成char类型。

                    之前讲过通过构造方法可以把字节数组转换成字符串,说明字节数组和字符串可以相互转换

            char[] toCharArray():把字符串转换为字符数组

                    之前讲过通过构造方法可以把字符数组转换成字符串,说明字符数组和字符串可以相互转换

            static String valueOf(char[] chs):把字符数组转换成字符串。通过静态方法转换(比通过构造方法转换的要好,推荐使用)

public class StringDemo {

    public static void main(String[] args) {

        char[] chArray = {'A','B','C'};

        String str1 = String.valueOf(chArray);

        System.out.println(str1);

        String str2 = new String(chArray);

        System.out.println(str2);

    }

}

            static String valueOf(int i):把int类型的数据转换成字符串

                    注意:String类的valueOf方法可以把任意类型的数据转换成字符串 ,详情请查看API

            String toLowerCase():把字符串转成小写,产生了一个新的串,字符串本身不变。

            String toUpperCase():把字符串转成大写,产生了一个新的串,字符串本身不变。

            String concat(String str):把字符串拼接

                

        D:其他功能

            a:替换功能

                String replace(char oldChar,char newChar)

String replace(CharSequence target, CharSequence replacement) //String replace(String old,String new)

例如,用 "b" 替换字符串 "aaa" 中的 "aa" 将生成 "ba" 而不是 "ab"。

            b:去空格功能

                String trim():    返回:此字符串移除了前导和尾部空白的副本;如果没有前导和尾部空白,则返回此字符串。

            c:按字典比较功能

                int compareTo(String str)// 该比较基于字符串中各个字符的 Unicode 值

                int compareToIgnoreCase(String str)

            

    自己写的compareTo方法:

        

    String类的compareTo方法的源码解析:

        

 

(6)    字符串的案例

        A:模拟用户登录,登录成功后开始猜数游戏

        B:字符串遍历

        C:统计字符串中大写,小写及数字字符的个数

        D:把字符串的首字母转成大写,其他小写

        E:把int数组拼接成一个指定格式的字符串

        F:字符串反转

        G:统计大串中小串出现的次数

 

案例A:模拟用户登录,登录成功后开始猜数游戏

 

案例B:字符串遍历

public class StringDemo {

    public static void main(String[] args) {

        String str = "HelloWord";

 

        //方法一

        for (int i = 0; i < str.length(); i++) {

            char ch = str.charAt(i);

            System.out.print(ch + " ");

        }

        System.out.println();

 

        //方法二

        byte[] byArray = str.getBytes();

        for (int i = 0; i < str.length(); i++) {

            System.out.print((char)byArray[i] + " ");

        }

        System.out.println();

        System.out.println(byArray.length);

 

        //方法三

        char[] chArray = str.toCharArray();

        for (int i = 0; i < str.length(); i++) {

            System.out.print(chArray[i] + " ");

        }

        System.out.println();

        

        System.out.println(chArray.length);

        

    }

}

案例C:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)

 

案例D:把字符串的首字母转成大写,其他小写

    

 

案例E:把int数组拼接成一个指定格式的字符串

    

 

案例F:字符串反转

分析:

A:键盘录入一个字符串

B:定义一个新字符串

C:倒着遍历字符串,得到每一个字符

a:length()和charAt()结合

b:把字符串转成字符数组

D:用新字符串把每一个字符拼接起来

E:输出新串

        

 

案例G:统计大串中小串出现的次数

    

 

猜你喜欢

转载自www.cnblogs.com/zhaolanqi/p/9237169.html