Commonly used methods of the String class:

Common methods of the String class:

int length (): returns the length of a string: return value.length
char the charAt (int index) : Returns the character return value [index] index of a
boolean isEmpty (): determining whether an empty string is: return value.length = 0 =
string toLowerCase (): use the default locale, all characters are converted to lowercase string
string toUpperCase (): use the default locale, string converts all characters to uppercase
string trim (): returns a copy of the string , ignores leading and trailing whitespace
boolean equals (Object obj): compare string contents are the same
boolean equalsIgnoreCase (string anotherString): similar to the equals method, the content is the same as comparing strings ignoring case

String concat (String str): The specified string is connected to this end of the string. Is equivalent to a "+"
int the compareTo (String anotherString): Compares two strings size
String substring (int beginIndex): Returns a new string, this string which is taken from the start to the last beginIndex a child string. (To the current character in the string)
String the substring (int beginIndex, int endIndex): Returns a new string, this string which is taken to start from beginIndex endIndex (not included) of a substring.

    @Test
    public void test2(){
        String s1="HelloWorld";
        String s2="helloworld";
        System.out.println (s1.equals (S2)); // if the contents of the string comparison same 
        System.out.println (s1.equalsIgnoreCase (S2)); // the comparison string is the same as the contents ignore case 

         String s3 = "ABC" ;
         S4 String = s3.concat ( "DEF"); // the specified string is connected to the end of the string 
        System.out.println (s4);

        String s5="abc";
        String s6=new String("abe");
        System.out.println (s5.compareTo (S6)); // compare the size of the two strings


        S7 String = "I am a chicken dish java ohh ohh hum" ;
        S8 String = s7.substring (2); // start from a position intercepting the string 
        System.out.println (s8);

      S9 String = s7.substring (3,12); // start capturing from a position to the end position 
        System.out.println (s9);

    }

    @Test
    public void test1(){

        String s1="HelloWrold";
        System.out.println(s1.length());
        System.out.println(s1.charAt(0));
        System.out.println(s1.charAt(9));
        System.out.println(s1.isEmpty());




        S2 String = s1.toLowerCase (); // character lowercase 
        System.out.println ( "s1 is not made or the original" + s1); // or the original string s1 immutable 
        System.out. println ( "after the change lowercase string:" + S2); // string lowercase after change


        String s3=" he llo world ";
        S4 String = s3.trim (); // end-users to register for the blank usually account when 
        System.out.println ( "------" + s3 + "----------" ); // go blank before 
        System.out.println ( "------" + S4 + "----------"); // the table to empty



    }

boolean endsWith (String suffix): Tests if this string ends with the specified suffix
boolean startsWith (String prefix): Tests if this string to the specified prefix beginning
boolean startsWith (String prefix, int toffset ): Tests if this string from the specified whether substring based index starts with the specified prefix

boolean contains (CharSequence s): if and only if this string contains the specified sequence of char values, return to true
int indexOf (String STR): Returns the index of the substring in the first occurrence of this string
int indexOf (string str, int fromIndex): returns the substring index within this string of the first occurrence, starting at the specified index
int lastIndexOf (string str): returns a substring in this string of the rightmost index appear at the
int lastIndexOf (string str, int fromIndex ): returns a substring index within this string of the last occurrence, starting at the specified index of reverse search

Note: indexOf and lastIndexOf method returns -1 if not found all

 

@Test
   public void test3(){


    Str1 String = "HelloWorld" ;
        // test whether this string ending specified suffix 
   Boolean B1 = str1.endsWith ( "RLD" );
       System.out.println(b1);



       boolean b2=str1.startsWith("he");
       System.out.println(b2);
        // test whether this string substring from the specified index starting with the prefix specified 
       Boolean B3 = str1.startsWith ( "LL", 2 );
       System.out.println(b3);

       String str2="wor";
       System.out.println (str1.contains (str2)); // if str1 contains str2 will return to true 
       System.out.println (str1.indexOf ( "lol" ));
       System.out.println(str1.indexOf("lo",5));

       String str3 = "hellorworld";

       System.out.println(str3.lastIndexOf("or"));
       System.out.println(str3.lastIndexOf("or",6));

       // What happens next, indexOf (str) and lastIndexOf (str) return the same value?
       // Case 1: there is only one str. Scenario 2: There is no str






   }

 

Alternatively:
String Replace (oldChar char, char newChar): Returns a new string, which is obtained by replacing all oldChar this string with newChar obtained.
String replace (CharSequence target, CharSequence replacement ): specified literal replacement sequence replacement substring of this string literal match all of the target sequence.
String replaceAll (String regex, String replacement ): given this string of all replacement alternative matches the given regular expression substring.
String replaceFirst (String regex, String replacement ): using the given first sub-alternative replacement of this string matches the given regular expression.
Matching:
boolean The matches (String regex): Tell whether this string matches the given regular expression.
Slice:
String [] Split (String REGEX): The given regular expression matching split this string.
String [] split (String regex, int limit): According to match a given regular expression to split this string, no more than a limit, if exceeded, all the rest are placed in the last element.

    @Test
    public void test4(){
        Str String = "I am a java dish chicken dish chicken dishes chicken dish chicken dishes chicken" ;
        Str2 String = str.replace ( 'vegetables', 'weak' );
        System.out.println(str);
        System.out.println(str2);


        Str3 String = str.replace ( "I", "you are" );
        System.out.println(str);
        System.out.println(str3);


        System.out.println("*************************");
        Str1 String = "12hello34world5java7891mysql456" ;
         // digital string replaced, that if there is the beginning and end result, then remove 
        String string = str1.replaceAll ( "\\ d +", ",") replaceAll ( ". ^, |, $ "," " );
        System.out.println(string);

        System.out.println("*************************");
        str1 = "12345" ;
         // determines whether all the string str digital composition, that is 1-n digits 
        Boolean The matches str1.matches = ( "\\ + D" );
        System.out.println(matches);
        Tel String = "0571-4534289" ;
         // determines whether this is a fixed telephone Hangzhou 
        Boolean Result = tel.matches ( "0571 - \\ D {7, 8}" );
        System.out.println(result);


        System.out.println("*************************");
        str1 = "hello|world|java";
        String[] strs = str1.split("\\|");
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
        System.out.println();
        str2 = "hello.world.java";
        String[] strs2 = str2.split("\\.");
        for (int i = 0; i < strs2.length; i++) {
            System.out.println(strs2[i]);
        }

    }

Complete code:

package String;

import org.junit.Test;

/**
 * Created with IntelliJ IDEA.
 * User: Guo Jinrong
 * Date: 2020/3/7 0007
 * Time: 22:27
 * E-mail: [email protected]
 * Class Description:
 */
public class StringMethodTest {

    /*
replace:
String replace (char oldChar, char newChar): Returns a new string, which is obtained by replacing all oldChar this string with newChar obtained.
String replace (CharSequence target, CharSequence replacement): specified literal replacement sequence replacement substring of this string literal match all of the target sequence.
String replaceAll (String regex, String replacement): replacing the given replacement string matches all given regular expression substring.
String replaceFirst (String regex, String replacement): using the given first sub-alternative replacement of this string matches the given regular expression.
match:
boolean matches (String regex): inform this string matches the given regular expression.
slice:
String [] split (String regex): The given regular expression matching split this string.
String [] split (String regex, int limit): According to match a given regular expression to split this string, no more than a limit, if exceeded, all the rest are placed in the last element.

 */
    @Test
    public  void test4 () {
        Str String = "I am a java dish chicken dish chicken dishes chicken dish chicken dishes chicken" ;
        Str2 String = str.replace ( 'vegetables', 'weak' );
        System.out.println(str);
        System.out.println(str2);


        Str3 String = str.replace ( "I", "you are" );
        System.out.println(str);
        System.out.println(str3);


        System.out.println("*************************");
        Str1 String = "12hello34world5java7891mysql456" ;
         // digital string replaced, that if there is the beginning and end result, then remove 
        String string = str1.replaceAll ( "\\ d +", ",") replaceAll ( ". ^, |, $ "," " );
        System.out.println(string);

        System.out.println("*************************");
        str1 = "12345" ;
         // determines whether all the string str digital composition, that is 1-n digits 
        Boolean The matches str1.matches = ( "\\ + D" );
        System.out.println(matches);
        Tel String = "0571-4534289" ;
         // determines whether this is a fixed telephone Hangzhou 
        Boolean Result = tel.matches ( "0571 - \\ D {7, 8}" );
        System.out.println(result);


        System.out.println("*************************");
        str1 = "hello|world|java";
        String[] strs = str1.split("\\|");
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
        System.out.println();
        str2 = "hello.world.java";
        String[] strs2 = str2.split("\\.");
        for (int i = 0; i < strs2.length; i++) {
            System.out.println(strs2[i]);
        }

    }

    /*
boolean endsWith (String suffix): Tests if this string ends with the specified suffix
boolean startsWith (String prefix): Tests if this string to the specified prefix beginning
boolean startsWith (String prefix, int toffset): Tests if this string substring from the specified whether the index starts with the specified prefix

boolean contains (CharSequence s): if and only if this string contains the specified sequence of char values, return true
int indexOf (String str): Returns the index of the substring in the first occurrence of the string
int indexOf (String str, int fromIndex): Returns the substring index within this string of the first occurrence, starting at the specified index
int lastIndexOf (String str): Returns a substring index within this string of the rightmost occurrence
int lastIndexOf (String str, int fromIndex): Returns a substring index within this string of the last occurrence, starting at the specified index of reverse search

Note: indexOf and lastIndexOf method returns -1 if not found all

     */
   @Test
   public  void test3 () {


    Str1 String = "HelloWorld" ;
        // test whether this string ending specified suffix 
   Boolean B1 = str1.endsWith ( "RLD" );
       System.out.println(b1);



       boolean b2=str1.startsWith("he");
       System.out.println(b2);
        // test whether this string substring from the specified index starting with the prefix specified 
       Boolean B3 = str1.startsWith ( "LL", 2 );
       System.out.println(b3);

       String str2="wor";
       System.out.println (str1.contains (str2)); // if str1 contains str2 will return to true 
       System.out.println (str1.indexOf ( "lol" ));
       System.out.println(str1.indexOf("lo",5));

       String str3 = "hellorworld";

       System.out.println(str3.lastIndexOf("or"));
       System.out.println(str3.lastIndexOf("or",6));

       // What happens next, indexOf (str) and lastIndexOf (str) return the same value?
       // Case 1: there is only one str. Scenario 2: There is no str






   }


    /*
int length (): returns the length of a string: return value.length
char charAt (int index): Returns the index of the character of a return value [index]
boolean isEmpty (): determining whether an empty string is: return value.length == 0
String toLowerCase (): Use the default locale, String Converts all characters to lowercase
String toUpperCase (): Use the default locale, all characters are converted to uppercase String
String trim (): Returns a string copy ignores leading and trailing whitespace
boolean equals (Object obj): compare the contents of the string are the same
boolean equalsIgnoreCase (String anotherString): Similar to the equals method, the content is the same as comparing strings ignoring case
String concat (String str): The specified string is connected to this end of the string. It is equivalent to using the "+"
int compareTo (String anotherString): compare the size of two strings
String substring (int beginIndex): Returns a new string, this string which is taken from the start to the last beginIndex a substring.
String substring (int beginIndex, int endIndex): Returns a new string, which is taken from this string starts beginIndex to endIndex (not included) of a substring.

     */



    @Test
    public  void test2 () {
        String s1="HelloWorld";
        String s2="helloworld";
        System.out.println (s1.equals (S2)); // if the contents of the string comparison same 
        System.out.println (s1.equalsIgnoreCase (S2)); // the comparison string is the same as the contents ignore case 

         String s3 = "ABC" ;
         S4 String = s3.concat ( "DEF"); // the specified string is connected to the end of the string 
        System.out.println (s4);

        String s5="abc";
        String s6=new String("abe");
        System.out.println (s5.compareTo (S6)); // compare the size of the two strings


        S7 String = "I am a chicken dish java ohh ohh hum" ;
        S8 String = s7.substring (2); // start from a position intercepting the string 
        System.out.println (s8);

      S9 String = s7.substring (3,12); // start capturing from a position to the end position 
        System.out.println (s9);

    }

    @Test
    public void test1(){

        String s1="HelloWrold";
        System.out.println(s1.length());
        System.out.println(s1.charAt(0));
        System.out.println(s1.charAt(9));
        System.out.println(s1.isEmpty());




        S2 String = s1.toLowerCase (); // character lowercase 
        System.out.println ( "s1 is not made or the original" + s1); // or the original string s1 immutable 
        System.out. println ( "after the change lowercase string:" + S2); // string lowercase after change


        String s3=" he llo world ";
        S4 String = s3.trim (); // end-users to register for the blank usually account when 
        System.out.println ( "------" + s3 + "----------" ); // go blank before 
        System.out.println ( "------" + S4 + "----------"); // the table to empty



    }

}

 

Guess you like

Origin www.cnblogs.com/jinronga/p/12439324.html