Notes d'étude de base Java Classe _String

table des matières

1. Vue d'ensemble

2. Fonction de jugement de la classe String

3. Obtenir la fonction de la classe String

4. Fonction de conversion de la classe String


1. Vue d'ensemble

Chaque objet chaîne est une constante.

2. Fonction de jugement de la classe String

String(byte[]):构造一个String类型的对象,将指定字节数组中的数据转化成字符串。
String(char[]):构造一个String类型的对象,将指定字符数组中的数据转化成字符串。
boolean equals(String):判断当前字符串与给定字符串是否相同,区分大小写。
boolean equalsIgnoreCase(String):判断当前字符串与给定字符串是否相同,不区分大小写。
boolean startWith(String):判断是否以给定字符串开头。

3. Obtenir la fonction de la classe String

int length():获取当前字符串的长度。
char charAt(int):获取指定索引位置的字符。
int indexOf(String):获取指定字符(串)第一次出现的索引。
int lastIndexOf(String):获取指定字符(串)最后一次出现的索引。
String substring(int):获取指定索引位置(含)之后的字符串。
String substring(int, int):获取从索引start位置(含)起至索引end位置(不含)的字符串。
public class Test {
    public static void main(String[] args) {
        //String(byte[]):构造一个String类型的对象,将指定字节数组中的数据转化成字符串。
        byte[] bs1 = {97, 98, 99};
        String str0 = new String(bs1);
        System.out.println(str0);
        System.out.println("-----------------------");

        //String(char[]):构造一个String类型的对象,将指定字符数组中的数据转化成字符串。
        char[] cs1 = {'c', 'b', 'a'};
        String str1 = new String(cs1);
        System.out.println(str1);
        System.out.println("-----------------------");


        String str2 = "i_like_typing";
        String str3 = "i_like_typing";
        String str4 = "I_LIKE_TYPING";
        //boolean equals(String):判断当前字符串与给定字符串是否相同,区分大小写。
        System.out.println(str2.equals(str3));
        System.out.println("-----------------------");
        //boolean equalsIgnoreCase(String):判断当前字符串与给定字符串是否相同,不区分大小写。
        System.out.println(str2.equalsIgnoreCase(str4));
        System.out.println("-----------------------");
        //boolean startWith(String):判断是否以给定字符串开头。
        System.out.println(str2.startsWith("i_like"));
        System.out.println("-----------------------");


        //int length():获取当前字符串的长度。
        System.out.println(str2.length());
        System.out.println("-----------------------");
        //char charAt(int):获取指定索引位置的字符。
        System.out.println(str2.charAt(0));
        System.out.println("-----------------------");
        //int indexOf(String):获取指定字符(串)第一次出现的索引。
        System.out.println(str2.indexOf("l"));
        System.out.println("-----------------------");
        //int lastIndexOf(String):获取指定字符(串)最后一次出现的索引。
        System.out.println(str2.lastIndexOf("g"));
        System.out.println("-----------------------");
        //String substring(int):获取指定索引位置(含)之后的字符串。
        System.out.println(str2.substring(3));

        //String substring(int, int):获取从索引start位置(含)起至索引end位置(不含)的字符串。
        System.out.println(str2.substring(1,3));
    }
}

La sortie est la suivante:

abc
-----------------------
cba
-----------------------
true
-----------------------
true
-----------------------
true
-----------------------
13
-----------------------
i
-----------------------
2
-----------------------
12
-----------------------
ike_typing
_l

 

4. Fonction de conversion de la classe String

byte[] getBytes():将字符串转换成字节数组。
char[] toCharArray():将字符串转换成字符数组。
static String valueOf():将指定类型数据转换成字符串。
String replace(old, new):将指定字符(串)替换成新的字符(串)。
String[] split(String):切割字符串,返回切割后的字符串数据,原字符串不变。
String trim():去掉字符串两端的空白字符。
public class Test3 {
    public static void main(String[] args) {
        //定义一个变量
        String s1 = "abc";

        //byte[] getBytes():将字符串转换成字节数组。
        byte[]  bys = s1.getBytes(); //97, 98, 99
        for (int i = 0; i < bys.length; i++) {
            System.out.println(bys[i]);
        }
        System.out.println("--------------------------");

        //char[] toCharArray():将字符串转换成字符数组。
        char[] chs = s1.toCharArray(); //'a', 'b', 'c'
        for (int i = 0; i < chs.length; i++) {
            System.out.println(chs[i]);
        }
        System.out.println("--------------------------");

        //static String valueOf():将指定类型数据转换成字符串。
        //整数123-->字符串"123"
        String s2 = String.valueOf(123);
        System.out.println(s2 + 4); //"1234"
        //实际开发中,上述方法都会用以下方法替代。
        String s3 = "" + 123;
        System.out.println(s3 + 4); // "1234"
        System.out.println("--------------------------");

        //String replace(old, new): 将指定字符(串)替换成新的字符(串)。
        String s4 = "abc abc abc";
        //'d' 替换 'b'
        String s5 = s4.replace('b', 'd');
        System.out.println("s5:" + s5);
        System.out.println("--------------------------");

        //String[] split(String) : 切割字符串,返回切割后的字符串数据,原字符串不变
        //将字符串s4按照空格进行切割
        //"abc", "abc", "abc"
        String[] arr = s4.split(" ");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        System.out.println("--------------------------");


        //String trim(): 去掉字符串两端的空白字符
        String s6 = "  a  b  c  ";
        String s7 = s6.trim();
        System.out.println("s6:" + s6);
        System.out.println("s7:" + s7);
    }
}

 La sortie est la suivante:

97
98
99
--------------------------
a
b
c
--------------------------
1234
1234
--------------------------
s5:adc adc adc
--------------------------
abc
abc
abc
--------------------------
s6:  a  b  c  
s7:a  b  c

 

Je suppose que tu aimes

Origine blog.csdn.net/qq_43191910/article/details/113611283
conseillé
Classement