Common methods in the String class

String class

No need to guide package

  1. In Java, any data quoted with double quotation marks is an object of String

    String s = "abc";
    

    String()

    Initialize a newly created String object so that it represents an empty character sequence.

    String(String original)

    Initialize a newly created String object so that it represents the same character sequence as the parameter list: in other words, the string created by the heart is a copy of the parameter string

    string(char[] value)

    Allocate a new String to represent the character sequence currently contained in the character array parameter

    String(char[] value,int offset,int count)

    Allocate a new String, which contains the characters taken from a word array of the string array parameter.

    String(byte[] bytes)

    Construct a new String by using the platform's default character set to decode the specified byte array

    String(byte[] bytes ,int offset,int length)

    Construct a new String by using the platform's default character set to decode the specified byte sub-array

    Case study

    public class StringConstructor {
          
          
        public static void main(String[] args) {
          
          
             // 1. 创建出一个空字符串序列  String()
             String s = new String(); // String s = "";
             System.out.println(s + "---");
    
             // 2. String(字符序列)
            String s1 = new String("hello");// String s = "hello"
            System.out.println(s1);// hello
    
          // 3. String(char[] ch) : 将参数char类型数组中的每一个字符, 拼接成一个字符串结果
            char[] ch = {
          
          'A','a','1','?','家'};
            String s2 = new String(ch);
            System.out.println(s2);// Aa1?家
    
            // 4. String(char[] ch, int beginIndex, int count): 将字符数组的一部分,转换成字符串
            // beginIndex 从指定索引位置开始
            // count 需要转换的字符个数
            String s3 = new String(ch,2,2);
            System.out.println(s3);// 1?
    
           // 5. String(byte[] b) : 将字节数组b, 参考编码表, 转换成字符, 将字符拼接成字符串
            byte[] b = {
          
          65,66,67,68};
            String s4 = new String(b);
            System.out.println(s4);// ABCD
    
            // 6. String(byte[] b, int beginIndex,int count): 将字节数组的一部分,转换成字符串
            String s5 = new String(b,1,2);
            System.out.println(s5);// BC
        }
    

Get function in String class

lenght():

lenght(): Get the number of characters in a string (the length of the string), the return value type is int

charAt(int index):

charAt(int index): Get the corresponding character at the specified index index position in the string, and the return value type is char

subString(int beginIndex):

subString(int beginIndex): cut out a part of the string, from the beginIndex index to the end of all character sequences are taken out, the return value result, the new string String

subString(int beginIndex, int endIndex) :

subString(int beginIndex, int endIndex): intercept the character sequence from beginIndex to endIndex-1 into a new string, and return the result String.
Note: In the JDK method, if it is to operate on two index positions, usually Including start index, excluding end index

indexOf(String str) :

indexOf(String str): Get the index position of the first occurrence of the parameter string str in the method call string. The
return value type is int. If the position of the parameter str is not found, return -1

indexOf(String str, int formIndex):

indexOf(String str, int formIndex): Returns the index of the first occurrence of the specified character in this string, starting from the specified index, the return value type is int, if the location of the parameter str is not found, it returns -1

lastIndexOf(String str) :

lastIndexOf(String str): Get the index position of the last occurrence of the parameter string str in the method call string
Return value type int, if the position of the parameter str is not found, return -1

Code

public class StringGetMethod {
    
    
    public static void main(String[] args) {
    
    
        // 1. 获取字符串长度 length()
        String s = "hello  ";
        System.out.println(s.length());// 7

        // 2. charAt(int index): 将字符串中指定的index索引位置上对对应的字符获取到
        // 返回值类型char
        // 因为字符串底层存储就是一个字符数组, 字符数组的索引就对应了字符串中索引位置
        char ch = s.charAt(4);
        System.out.println(ch);// 'o'
        System.out.println(s.charAt(5) + "---");// ' '

        // 扩展 : 利用字符串有索引, 范围0---length()-1, 使用charAt方法,将每一个索引位置上
        // 的字符获取到, 形成了字符串的遍历
        for(int index = 0; index < s.length(); index++){
    
    
            char c = s.charAt(index);
            System.out.println(c);
        }

        // 3. subString(int beginIndex) : 截取出字符串的一部分,从beginIndex索引开始, 到最后
        // 的全部字字符串取出来, 返回值结果,新的字符串
        String s1 = "我在家?hello";
        String s1Sub = s1.substring(4);
        System.out.println(s1Sub);// hello

        // 4. subString(int beginIndex, int endIndex) : 从beginIndex到endIndex-1之间的字符序列截取
        // 成一个新的字符串, 返回值结果String
        // 注意 : JDK的方法中, 如果是对于两个索引位置进行操作, 通常包括开始索引, 不包括结束索引
        System.out.println(s1.substring(1,5));// 在家?h

        // 5. indexOf(String str) : 获取参数字符串str在方法调用字符串中第一次出现的索引位置
        // 返回值类型int, 如果没有找到参数str存在位置, 返回-1
        int index1 = s1.indexOf("he");
        System.out.println(index1);// 4

        int index2 = s1.indexOf("le");
        System.out.println(index2);// -1

        // 6. indexOf(String str, int formIndex):
        // 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
        int index3 = s1.indexOf("l",7);
        System.out.println(index3);// 7

        System.out.println("-----------------");

        // 7. lastIndexOf(String str) : 获取参数字符串str在方法调用字符串中最后一次出现的索引位置
        // 返回值类型int, 如果没有找到参数str存在位置, 返回-1
        System.out.println(s1.lastIndexOf("l"));// 7
        System.out.println(s1.lastIndexOf("哈哈"));// -1
    }

Conversion function

  1. byte[] getBytes(): Convert the current string into a byte array

  2. char[] toCharArray(): Convert the current string into a character array

  3. toUpperCase(): Convert the current string to all uppercase

  4. toLowerCase(): Convert the current string to all lowercase

  5. Static valueOf family: data of any data type can be converted into a string

Other functions

1. Replace(String oldStr, String newStr): replace the old string in the caller with a new string, and the return value is the new string after replacement

2. Split(String regex): Cut the string according to the parameter string regex rule, and the result is a String[]

3. trim(): remove the spaces and tabs on the left and right sides of the string

Guess you like

Origin blog.csdn.net/weixin_56204788/article/details/115360698