一、字符串类

1、字符串是最常用的一种数据类型,表现形式有两种:String和StringBuffer
2、String是一种引用类型,也是final类型。
3、String对象代表一组不可改变的Unicode字符序列,对它的任何修改实际上又产生一个新的字符串,String类对象的内容一旦被初始化就不能再改变。
4、Stringbuffer对象代表一组可改变的Unicode字符序列。Stringbuffer实质上相当于字符串缓冲区,内容可修改可变,但是对象不变。

二、String创建方式

(一)静态方式(常用)

静态方式(常用)创建String像是给变量赋值一样来使用。
如:

String s1="abc";
String s2="abc";

java定义动态字符串数组并赋值 java动态字符串有哪些类_java定义动态字符串数组并赋值


使用静态方式创建的字符串,在方法区的常量池中只会产生唯一一个字符串对象,使用该方式产生同样一个字符串时,内存中不再开辟另外一块空间,而是两个应用变量指向同一个字符串对象。

(二)动态方式
动态方式创建String是使用new运算符,动态地分配内存,在堆内存中会产生出不同的对象。每new一个对象就分配一个内存空间。
如:

String s1=new String("abc");
Satring s2=new String("abc");

java定义动态字符串数组并赋值 java动态字符串有哪些类_java定义动态字符串数组并赋值_02

三、String对象的创建

1、String 变量名=new String();
初始化一个新创建的String对象,它表示一个空字符序列。

2、String 变量名=new String(String value);
初始化一个新创建的String对象,表示一个与该参数相同的字符序列。

3、String 变量名=new String(new(StringBuffer buf));
分配一个新的字符串,它包含当前包含在字符串缓冲区参数中的字符序列。
实质就是将StringBuffer的内容转换为String类型的内容

4、String 变量名=“Hello”(常用)
字符串文字常量本身是一个String对象

public class StringTest {
    public static void main(String[] args) {
    //先新建,后赋值使用
    String str1=new String();
    str1="str1";

    //创建时赋值
    String str2=new String("str2");

    //将StringBuffer的内容转换为String类型的内容
    String str3=new String(new StringBuilder("str3"));

    //直接赋值(常用)
    String str4="str4";

    System.out.println(str1);
    System.out.println(str2);
    System.out.println(str3);
    System.out.println(str4);
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_System_03

四、String的常用方法

1、连接操作符“+”

连接操作符“+”可以将其它各种类型的数据转换成字符串,并前后连接成新的字符串。
任何与字符串相“+”的结果都是字符串。
每“+”一个就创建一个新的对象,大量消耗内存,一般用于3到5个的“+”,循环千万别用。

public class StringTest {
    public static void main(String[] args) {
    String str1="Hello";
    String str2="World";
    //用连接操作符“+”,对str1和str2进行连接操作
    String str=str1+str2;
    System.out.println(str);
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_System_04

对字符串的操作,串的操作,通常使用StringBuffer对字符串进行操作,然后再转换成String类型结果进行输出。
利用StringBuffer字符串缓冲区内容可修改,对象不变的特点,能节省内存的消耗。

用StringBuffer的apped( )代替“+”

在原来字符串的基础上在其后面追加新的字符串组成新的字符串。内容修改,但是对象不变。

public class StringTest {
    public static void main(String[] args) {

    StringBuffer str1=new StringBuffer("Hello");
    StringBuffer str2=new StringBuffer("World");
    //在str1后面追加str2
    StringBuffer str3=str1.append(str2);
    String str=str3.toString();
    System.out.println(str);
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_System_05

2、boolean equals(String)

判断两个字符串对象的内容是否相等。

public class StringTest {
    public static void main(String[] args) {
        String a="abc123";
        String b="abc123";
        String c="123abc";
        //判断两个字符串对象的内容是否相等。
        boolean resultAB=a.equals(b);
        boolean resultAC=a.equals(c);
        System.out.println(resultAB);
        System.out.println(resultAC);
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_字符串_06

3、boolean equalsIgnoreCase(String)

比较两个字符串的内容是否相等,忽略大小写。

public class StringTest {
    public static void main(String[] args) {
        String a="abc123";
        String b="ABC123";
        String c="123abc";
        //判断两个字符串对象的内容是否相等,忽略大小写
        boolean resultAB=a.equalsIgnoreCase(b);
        boolean resultAC=a.equalsIgnoreCase(c);
        System.out.println(resultAB);
        System.out.println(resultAC);
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_java定义动态字符串数组并赋值_07

4、String toUpperCase( )

将String对象中的所有字符都转换为大写。

public class StringTest {
    public static void main(String[] args) {
        String a="abc123";
        String b="123abc";
        //将String对象中的所有字符都转换为大写。
        String A=a.toUpperCase();
        String B=b.toUpperCase();
        System.out.println(A);
        System.out.println(B);
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_java_08

5、String toLowerCase( )

将String对象中的所有字符都转换为小写。

public class StringTest {
    public static void main(String[] args) {
        String a="ABC123";
        String b="123aBc";
        //将String对象中的所有字符都转换为小写。
        String A=a.toLowerCase();
        String B=b.toLowerCase();
        System.out.println(A);
        System.out.println(B);
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_string_09

6、char charAt(int)

返回指定索引处的char值。

public class StringTest {
    public static void main(String[] args) {
        String a="ABC123";
        String b="123aBc";
        //返回指定索引处的char值
        //返回a中索引2的char值,返回b中索引2的char值
        System.out.println(a.charAt(2));
        System.out.println(b.charAt(2));
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_字符串_10

7、String substring(int begin)

返回一个新字符串,该字符串是从begin开始的字符串的内容。

public class StringTest {
    public static void main(String[] args) {
        String a="ABC123";
        String b="123aBc";
        //返回一个新字符串,该字符串是从begin开始的字符串的内容。
        //返回一个新字符串,该字符串是a从2开始的字符串的内容。
        //返回一个新字符串,该字符串是b从2开始的字符串的内容。
        System.out.println(a.substring(2));
        System.out.println(b.substring(2));
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_字符串_11

8、String substring(int begin,int end)

返回一个新字符串,该字符串是从begin开始到end-1结束的字符串的内容。
不包含结束下标所对应的元素。

public class StringTest {
    public static void main(String[] args) {
        String a="ABC123";
        String b="123aBc";
        //返回一个新字符串,该字符串是从begin开始到end-1结束的字符串的内容。
        //返回一个新字符串,该字符串是a从3开始到5的字符串的内容。
        //返回一个新字符串,该字符串是b从3开始到5的字符串的内容。
        System.out.println(a.substring(3, 6));
        System.out.println(b.substring(3,6));
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_字符串_12

9、int indexOF/lastIndexOf(char)

返回指定字符在此字符串中第一次/最后一次出现处的索引。

public class StringTest {
    public static void main(String[] args) {
        String a="ABC123ABC";
        String b="aBc123aBc";
        //返回指定字符在此字符串中第一次/最后一次出现处的索引。
        //返回C在a字符串中第一次出现处的索引。
        //返回B在b字符串中最后一次出现处的索引。
        System.out.println(a.indexOf('C'));
        System.out.println(b.lastIndexOf('B'));
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_System_13

10、String trim( )

返回新的字符串,忽略字符串的前面空白和尾部空白。但是字符串中间的不能去掉。

public class StringTest {
    public static void main(String[] args) {
        String a="   ABC 123 ABC   ";
        String b="  aBc  123 aBc   ";
        //返回新的字符串,忽略字符串的前面空白和尾部空白。但是字符串中间的不能去掉。
        System.out.println(a);
        System.out.println(a.trim());

        System.out.println(b);
        System.out.println(b.trim());
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_字符串_14

11、int length()

返回此字符串的长度(字符元素的个数)。

public class StringTest {
    public static void main(String[] args) {
        String a="ABC123ABC";
        String b="aBc123aBc";
        //返回此字符串的长度(字符元素的个数)。
        System.out.println(a.length());
        System.out.println(b.length());
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_java定义动态字符串数组并赋值_15

12、String concat(String str)

将指定字符串连接到此字符串的结尾。

public class StringTest {
    public static void main(String[] args) {
        String a="Hello";
        String b="World";
        //将指定字符串连接到此字符串的结尾。
        //将b字符串连接到a字符串的结尾
        System.out.println(a.concat(b));

    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_字符串_16

13、byte[ ] getBytes( )

使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

public class StringTest {
    public static void main(String[] args) {
        String a="Hello";
        String b="World";
        //使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
        byte [] c=a.getBytes();
        System.out.println(c);

    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_System_17

14、String [ ] split (String regex)

根据给定正则表达式的匹配拆分此字符串。
String regex:拆分的标志

public class StringTest {
    public static void main(String[] args) {
        String a="192.168.1.1";
        //根据给定正则表达式的匹配拆分此字符串
        //根据给定"."拆分IP
        String ips[]=a.split("\\."); 
        for(int i=0;i<ips.length;i++)
        {
            System.out.print(ips[i]+" ");
        }
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_string_18

15、String replace(char oldChar.char newChar)

返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的。
不是新建新的对象,而是在原来的字符上做修改。

public class StringTest {
    public static void main(String[] args) {
        //返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的.
        //将Tom替换成Junny
        String a="Hello,Tom!";
        String b=a.replace("Tom", "Junny");
        System.out.println(b);
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_System_19

16、boolean startsWhit(String prefix) / endsWhit(String suffix)

测试此字符串是否以指定的前缀/后缀开始/结束。

public class StringTest {
    public static void main(String[] args) {
        //测试此字符串是否以指定的前缀/后缀开始/结束
        String a="Hello,Tom";
        String b="Hello,Junny";
        //测试a字符串是否以"Hello"开始
        System.out.println(a.startsWith("Hello"));
        //测试b字符串是否以"Tom"结束
        System.out.println(b.endsWith("Tom"));
    }
}

java定义动态字符串数组并赋值 java动态字符串有哪些类_java定义动态字符串数组并赋值_20