Common methods of String class

Posted by thedarkwinter on Mon, 20 Dec 2021 14:39:11 +0100

  • int length(): returns the length of the string.
  • char charAt(int index): returns the character at an index.
  • boolean isEmpty(): judge whether the string is empty.
  • String toLowerCase(): all characters are converted to lowercase.
  • String toUpperCase(): all characters are capitalized.
  • String trim(): returns a copy of a string, ignoring leading and trailing whitespace.
    @Test
    public void test1(){
        String s1 = "HelloWorld";
        System.out.println(s1.length());//10

        System.out.println(s1.charAt(0));//H
        System.out.println(s1.charAt(9));//d

        String s2="";
        System.out.println(s1.isEmpty());//false
        System.out.println(s2.isEmpty());//true


        System.out.println(s1.toLowerCase());//helloworld
        System.out.println(s1.toUpperCase());//HELLOWORLD
        System.out.println(s1);//s1 itself has not changed

        String s3="   he llo world    ";
        System.out.println(s3.trim());//he llo world
    }
  • boolean equals(Object obj): compare whether the contents of strings are equal.
  • boolean equalsignorecase (string otherstring): compare whether the contents of strings are equal, ignoring case.
  • String concat(String str): connects the specified string to the end of the string, equivalent to +.
  • Int CompareTo (string otherstring): compares the size of two strings.
  • String substring (int beginIndex): returns a new string, which is a substring intercepted from beginIndex.
  • String substring (int beginIndex, int endindex): returns a new string, which is a substring intercepted from beginIndex to endindex (not included).
    @Test
    public void test2(){
        String s1 = "HelloWorld";
        String s2 = "helloworld";

        System.out.println(s1.equals(s2));//false
        System.out.println(s1.equalsIgnoreCase(s2));//true

        String s3 ="abc";
        String s4 =s3.concat("def");
        System.out.println(s4);//abcdef

        String s5 ="abc";
        String s6 =new String("abe");
        System.out.println(s5.compareTo(s6));//-2

        System.out.println(s1.substring(2));//lloWorld
        System.out.println(s1.substring(2,5));//llo, left closed right open [)
    }
  • boolean endsWith(String suffix): tests whether the string ends with the specified suffix.
  • boolean startsWith(String prefix): test whether this string starts with the specified prefix.
  • boolean startsWith(String prefix, int toffset): test whether the string starts with the specified prefix when it is separated from the specified index.
  • Boolean contain (charsequences): returns true if and only if the sub character contains the specified char value sequence.
  • int indexOf(String str): returns the index of the first occurrence of the specified substring in this string.
  • int indexOf(String str,int fromIndex): returns the index of the first occurrence of the specified substring in this string, and searches from the specified index.
  • int lastIndexOf(String str): returns the index of the rightmost occurrence of the specified substring in this string.
  • int lastIndexOf(String str,int fromIndex): returns the index of the last occurrence of the specified substring in this string, and performs reverse search from the specified index.
    @Test
    public void test3() {
        String s1 = "HelloWorld";
        System.out.println(s1.endsWith("rld"));//true
        System.out.println(s1.startsWith("hel"));//false
        System.out.println(s1.startsWith("llo", 2));//true

        System.out.println(s1.contains("oWo"));//true

        System.out.println(s1.indexOf("lo"));//3
        System.out.println(s1.indexOf("lol"));//-1

        System.out.println(s1.indexOf("lo",5));//-1

        String s2 ="hellorworld";
        System.out.println(s2.lastIndexOf("or"));//7
        System.out.println(s2.lastIndexOf("or",6));//4
    }

Replace

  • String replace(char oldChar,char newChar): returns a new string obtained by replacing all oldchars in the string with newChar.
  • String replace(CharSequence target,CharSequence replacement): replace the substring specified in the string with another string.
  • String replaceAll(String regex,String replacement): replaces this string with the given replacement
  • String replaceFirst(String regex,String replacement)

matching

  • boolean matches(String regex): judge whether the string matches the given regular expression.

section

  • String [] split(String regex): splits the string according to the matching of the given regular expression.
  • String [] split(String regex,int limit): split the string according to the matching given regular expression. The maximum number is no more than limit. If it exceeds the limit, all the rest will be placed in the last element.
        @Test
        public void test4() {
            String s1 = "China USA France";
            System.out.println(s1.replace("method","virtue"));
            System.out.println(s1.replace("U.S.A","Canada"));
    
            String s2 ="12hello34world5java7891mysql456";
            //Replace the number in the string with and remove it if there is a at the beginning and end of the result
            String s3 =s2.replaceAll("\\d+",",").replaceAll("^,|,$","");
            System.out.println(s3);//hello,world,java,mysql
    
            String s4 ="12345";
            //Determines whether the string consists entirely of arrays
            System.out.println(s4.matches("\\d+"));
    
            String s5 ="0517-1234567";
            //Determine whether to use Hangzhou's fixed number
            System.out.println(s5.matches("0517-\\d+"));
    
            String s6 ="hello|world|java|mysql";
            String[] strs =s6.split("\\|");
            for (int i = 0; i < strs.length; i++) {
                System.out.println(strs[i]); //hello world java mysql
            }
    
            String[] str2 =s6.split("\\|",3);
            for (int i = 0; i < str2.length; i++) {
                System.out.println(str2[i]); //hello world java|mysql
            }
        }

Topics: Java Back-end