Common methods of String class

Posted by numan82 on Sat, 11 Apr 2020 11:10:06 +0200

Method interpretative statement
char charAt(int index)
Returns the index character in the string
boolean equals(String other)
Returns true if the string is equal to another; otherwise, returns false.
boolean equalsIgnoreCase(String other)
Returns true if the string is equal to another (case ignored); otherwise, returns false.
int indexOf(String str)
Returns the index position of the first substring str in the string from scratch. If the substring str is not found, - 1 is returned.
lastIndexOf()
Returns the index position of the first substring str in the string from the end. If the substring str is not found, - 1 is returned.
int length()
Returns the length of the string.
String replace(char oldChar,char newChar)
Returns a new string generated by replacing all oldchars that appear in the string with newChar.
boolean startsWith(String prefix)
Returns true if the string starts with prefix.
boolean endsWith(String prefix)
Returns true if the string ends in prefix.
String substring(int beginIndex)
Returns a new string from the original string beginIndex to the end of the string.
String substring(int beginIndex,int endIndex)
Returns a new string containing all characters from the original string beginIndex to the end of the string or endIndex-1.
String toLowerCase()
Returns a new string that changes all uppercase letters in the original string to lowercase letters.
String toUpperCase()
Returns a new string that changes all lowercase letters in the original string to uppercase letters.
String trim()
Returns a new string that removes spaces at the beginning and end of the original string

Code test:

package demo4;

public class StringTest {
    public static void main(String[] args) {
        String s="abcd";
        String s1=new String("abcd");
        System.out.println(s==s1);  //  Address comparison results false
        System.out.println(s.equals(s1));//Value comparison results true
        System.out.println(s.substring(1));//Intercept from where subscript is 1
        System.out.println(s.substring(1,3));//Intercept from subscript 1 to the end of subscript 3 excluding 3
        String value="abcdefjf";
        System.out.println(value.charAt(3));//Index 3 is d
        System.out.println(value.replace("fj","&"));//hold fj replace with&
        System.out.println(value.indexOf("e"));//Character string e Index position 4 of
        System.out.println(value.indexOf("BB"));//Include this string, not return-1
        System.out.println(value.lastIndexOf("f"));//Character string f Last position 7
        System.out.println(value.startsWith("a"));//Whether to use a Start true
        System.out.println(value.endsWith("f"));//Whether to use f Ending
        String value1=" I love you !";
        System.out.println(value1.trim());//Remove all spaces
        System.out.println(value1.replace(" ",""));//Remove all spaces
        System.out.println(value1.toUpperCase());//String to uppercase
        System.out.println(value1.toLowerCase());//String to lowercase
    }
}

 

StringBuffer and StringBuilder

 

  • List of common methods:

 

  1. Overloaded public StringBuilder append( )Method to add a character sequence to the StringBuilder object, still returning its own object.

  2. Method public StringBuilder delete(int start,int end) can delete a character sequence from start to end-1, and still return its own object.

  3. Method public StringBuilder deleteCharAt(int index) removes the char at the specified location of this sequence and still returns its own object.

  4. Overloaded public StringBuilder insert( )Method can insert a character sequence for the StringBuilder object at a specified location and still return its own object.

  5. The method public StringBuilder reverse() is used to reverse the character sequence and still return its own object.

  6. Method public String toString() returns the string representation of the data in this sequence.

  7. Methods with similar meaning to String class:

 

public int indexOf(String str)

 

public int indexOf(String str,int fromIndex)

 

public String substring(int start)

 

public String substring(int start,int end)

 

public int length()

 

char charAt(int index)

 

  • String: immutable character sequence. A kind of

  • StringBuffer: variable character sequence, thread safe, but inefficient. A kind of

  • StringBuilder: variable character sequence, unsafe thread, but high efficiency (commonly used).

 

package com.sxt.demo;

public class TestString {
    public static void main(String[] args) {
        String s="abCde ";
        System.out.println(s.charAt(3));//Index 3 is d
        System.out.println(s.toLowerCase());//Change this string to lowercase
        System.out.println(s.toUpperCase());//Change this string to uppercase
        System.out.println(s.substring(0,3));//Intercept 0-3 Index string
        System.out.println(s.replace("d","o"));//replace
        System.out.println(s.trim());//Remove spaces
        System.out.println(s.lastIndexOf("e"));//The last one e Index locations that appear
        System.out.println(s.indexOf(3));//Whether the string contains 3, not return-1
        System.out.println(s.endsWith(" "));//Whether to end with a space
        System.out.println(s.startsWith("a"));//Whether to use a Start
        StringBuilder value=new StringBuilder();
        value.append("I am");
        value.append("One");
        value.append("Cute guy");//Append character meaning
        System.out.println(value);
        for(int i=1;i<=10;i++){
            value.append(i);
        }
        System.out.println(value);
        StringBuffer a=new StringBuffer("Happy forever");
        System.out.println(a.delete(4,6));//Index 4-6 Delete where
        System.out.println(a.insert(0,"I"));//Insert me at index 0
        System.out.println(a.insert(1,"Meeting"));//Insert at index 1
        System.out.println(a.reverse());//Reverse string
    }
}

 

Topics: Java