Some common methods in the String class in JAVA

Table of contents

String comparison method:

boolean equals(Object anObject):

 int compareTo(String s):

int compareToIgnoreCase(String str)

String search method:

char charAt(int index):

int indexOf(int ch):

 int indexOf(int ch, int fromIndex):

int indexOf(String str):

int indexOf(String str, int fromIndex):

int lastIndexOf(int ch):

int lastIndexOf(int ch, int fromIndex):

int lastIndexOf(String str):

int lastIndexOf(String str, int fromIndex):

String conversion

String.valueOf():

String toUpperCase();String toLowerCase():

char[] toCharArray();

String(char value[]):

String replacement method:

String replaceAll(String regex, String replacement):

String replaceFirst(String regex, String replacement)

String[] split(String regex):

String substring(int beginIndex, int endIndex):


 

String comparison method:

boolean equals(Object anObject):

Compares two strings for equality, returns true if equal, otherwise returns false

    public static void main(String[] args) {

        String a = "asdf";
        System.out.println(a.equals("aaa"));
        System.out.println(a.equals("asdf"));
    }

 int compareTo(String s):

To compare whether two strings are equal, first compare them in dictionary order. If unequal characters appear , directly return the difference in size between the two characters ; if the first k characters are equal (k is the minimum length of the two characters), Returns the difference between the lengths of two strings.

    public static void main(String[] args) {

        String a = "asdf";
        System.out.println(a.compareTo("aaa"));
        System.out.println(a.compareTo("asdf"));
        System.out.println(a.compareTo("asd"));

    }

int compareToIgnoreCase(String str)

Ignore the case of characters for comparison, and the return value rules are:

  • First, compare the size according to dictionary order. If there are unequal characters , directly return the size difference of the two characters ;
  • If the first k characters are equal (k is the minimum length of two characters), return the difference in length between the two strings.
    public static void main(String[] args) {

        String a = "asdf";
        System.out.println(a.compareToIgnoreCase("aaa"));
        System.out.println(a.compareToIgnoreCase("ASDF"));
        System.out.println(a.compareToIgnoreCase("asd"));

    }

String search method:

char charAt(int index):

Returns the character at index position . If index is negative or out of bounds, an IndexOutOfBoundsException exception is thrown.

    public static void main(String[] args) {

        String a = "asdf";
        System.out.println(a.charAt(0));
        System.out.println(a.charAt(3));
        System.out.println(a.charAt(5));

    }

 

int indexOf(int ch):

Returns the position where ch first appears , if not, returns -1

    public static void main(String[] args) {

        String a = "asdddf";
        System.out.println(a.indexOf('d'));
        System.out.println(a.indexOf('a'));
        System.out.println(a.indexOf('h'));

    }

 int indexOf(int ch, int fromIndex):

Find ch starting from the fromIndex position  and return the position of the first occurrence , without returning -1

    public static void main(String[] args) {

        String a = "asdddf";
        System.out.println(a.indexOf('d', 3));
        System.out.println(a.indexOf('a', 1));
        System.out.println(a.indexOf('h',0));

    }

int indexOf(String str):

Returns the position where str first appears , without returning -1

    public static void main(String[] args) {

        String a = "asdddf";
        System.out.println(a.indexOf("dd"));
        System.out.println(a.indexOf("ss"));

    }

int indexOf(String str, int fromIndex):

Find the first occurrence of str starting from the fromIndex position , without returning -1

    public static void main(String[] args) {

        String a = "asdddf";
        System.out.println(a.indexOf("dd", 3));
        System.out.println(a.indexOf("ss", 0));

    }

 

int lastIndexOf(int ch):

Search from back to front , return to the position where ch first appears , if not return -1

    public static void main(String[] args) {

        String a = "asdddf";
        System.out.println(a.lastIndexOf('d'));
        System.out.println(a.lastIndexOf('s'));
        System.out.println(a.lastIndexOf('v'));

    }

int lastIndexOf(int ch, int fromIndex):

Start searching from the fromIndex position, and search from back to front for the position where ch first appears . -1 is not returned.

    public static void main(String[] args) {

        String a = "asdddf";
        System.out.println(a.lastIndexOf('d', 2));
        System.out.println(a.lastIndexOf('d', 3));
        System.out.println(a.lastIndexOf('d', 4));

        System.out.println(a.lastIndexOf('g', 5));

    }

 

int lastIndexOf(String str):

Search from back to front and return the position where str first appears . -1 is not returned.

    public static void main(String[] args) {

        String a = "asdddf";
        System.out.println(a.lastIndexOf("dd"));
        System.out.println(a.lastIndexOf("as"));
        System.out.println(a.lastIndexOf("bv"));

    }

 

int lastIndexOf(String str, int fromIndex):

Find the position where str first appears from back to front . If the subscript of this position is not greater than fromIndex, return, otherwise continue to search forward . None returns -1

    public static void main(String[] args) {

        String a = "asdddf";
        System.out.println(a.lastIndexOf("dd", 3));
        System.out.println(a.lastIndexOf("dd", 2));
        System.out.println(a.lastIndexOf("dd", 1));
        System.out.println(a.lastIndexOf("as", 0));
        System.out.println(a.lastIndexOf("bv", 0));

    }

String conversion

String.valueOf():

Convert all basic type values ​​to string type

    public static void main(String[] args) {

        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf('a');
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

 

String toUpperCase();
​​​​​​​String toLowerCase():

Returns a new string with the original string converted to uppercase .

Returns a new string with the original string converted to lowercase .

    public static void main(String[] args) {
        String s1 = "heLLo";
        String s2 = "HEllO";
        System.out.println(s1.toUpperCase());
        System.out.println(s2.toLowerCase());
    }

char[] toCharArray();
String(char value[]):

Convert string to array ; original string will not be affected

Convert array to string ; original array will not be affected

    public static void main(String[] args) {
        String s = "hello";
        
        char[] ch = s.toCharArray();
        System.out.println(Arrays.toString(ch));
        
        String s2 = new String(ch);
        System.out.println(s2);
    }

String replacement method:

String replaceAll(String regex, String replacement):

Replace all specified content

    public static void main(String[] args) {

        String str = "helloworld" ;

        System.out.println(str.replaceAll("l", "O"));
    }

String replaceFirst(String regex, String replacement)

Replace first content

    public static void main(String[] args) {

        String str = "helloworld" ;

        System.out.println(str.replaceFirst("l", "O"));
    }

String[] split(String regex):

Split all strings

    public static void main(String[] args) {
        String str = "hello world hello" ;
        String[] result = str.split(" ") ; // 按照空格拆分
        for(String s: result) {
            System.out.println(s);
        }
    }

String substring(int beginIndex, int endIndex):

Intercept strings within the range of [beginIndex,endIndex)

    public static void main(String[] args) {
        String str = "helloworld" ;

        System.out.println(str.substring(0, 5));
    }

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/132778482