Some Common String Functions summary review

com.ethjava Package; 

public class Stringlianxi { 
    public static void main (String [] args) { 

        String STR = "The Great One are by You!"; 
        // the substring (int the beginIndex) form 
        // This method for extracting, from the position of the index to begin at the end portion of the string. 
        // Call When: brackets are to be extracted beginning of the string, the return value is a string method of extraction. 
        System.out.println (str.substring (4));! // are at The Great One 
        System.out.println (str.substring (3));! // are at The Great One is the first space 

        // ubstring (int beginIndex, int endIndex) form 
        // this method represents a starting index beginIndex taken, including the start character string taken of the character corresponding to the index; 
        // index endIndex indicates the end, the string is not included in the end, taken index corresponding character 
        System.out.println (str.substring (4, 6)); // Ar
 
        // string remove both spaces
        String str2 = "yxf are great! "; There are two spaces before and after //
        String str5 = "a girl";
        Str3 str2.trim = String (); 
        System.out.println (str3); // YXF are Great! 
        System.out.println (str2.length ()); // 16 
        System.out.println (str3.length ( )); // 14 
        // string name .length (); Get length of the string 

        // connection string 
        // use + number, use the "+" operator and connection strings int type (or double) data when "+" to int (or double) automatically converted to data type String. 
        String str4 = "Welcome to" + "Beijing" + 2019 + " Welcome to" + "Beijing."; 
        System.out.println (str4); 
        // toBeijing2019 is available for purchase Welcome to Beijing. 
        // use concat () method 
        // string 1.concat (String 2); 
        // string 2 is connected to one end of the string, to form a new string.  
        // a girl,
        System.out.println (str5.concat ( ", Hello")); 

        String STR6 = "abcdef I MNJKL"; 
        System.out.println (str6.toLowerCase ()); // output: abcdef I mnjkl 
        System. out.println (str6.toUpperCase ()); // output: ABCDEF I MNJKL 
        // non-alphanumeric characters are not affected 

        // string division 
        string = Colors "Red, Black, White, Yellow, Blue"; 
        string [ ] arr1 = Colors.split ( "," ); // number of elements is not limited 
        String [] arr2 = Colors.split ( " ,", 3); // number of elements is restricted. 3 
        System.out.println ( " All color: "); 
        for (int I = 0; I <arr1.length; I ++) { 
            System.out.println (of arr1 [I]);
        } 
        // All Colors: 
        // Red 
        // Black 
        // White 
        // Yellow 
        // Blue 
        System.out.println ( "the first three color:"); 
        for (int J = 0; J <arr2.length; J ++) { 
            System.out.println (arr2 is [J]); 
        } 
        // the first three color: 
        // Red 
        // Black 
        // White, Yellow, Blue 

        // string class split () method can be segmented by target string delimiter specified, the contents stored in an array of strings split in. This method has two main forms Overload follows: 
        //str.split(String Sign) 
        //str.split(String Sign, int limit) 
        // STR target string to split. 
        // sign specified delimiter can be any character string. 
        // limit represents the number of strings generated after segmentation limit, if not specified, it is no limit, until the entire target string is completely divided up. 
        // and ".", "|" Is the escape character, have to add "\\." 
        // If with "." As a separator, it must be written String.split ( "\\."), So as to properly separated, can not be used String.split ( ".").
        // if the "|" as a separator, it must be written String.split ( "\\ |"), in order to properly separated, can not be used String.split ( "|"). 

        // replace certain character (string) string 
        // Replace () method is used to target string specified character (string) is replaced with a new character (string), syntax is as follows: 
        // String .replace (occurrences of oldChar in string, string newChar) 
        // where, oldChar represents a string to be replaced; newChar string representing for replacement. replace () method will be replaced by a string of all oldChar newChar. 
        // for a single character or string 
        String A = "the Hello the Java, the Hello Me \\\\!"; 
        System.out.println (a.replace ( "the Hello", "hello")); // you ! good java, hello \\ Me 
        System.out.println (a.replaceFirst ( "the hello", "hello"));! // hello java, the hello Me \\ 
        System.out.println (a.replaceAll ( "hello", "good")); // good java, good \\ Me! 
        System.out.println (A.
        System.out.println (a.replace (,) "!" "?");? // the Hello the Java, the Hello Me \\ 
        System.out.println (a.replace ( "\\", "\\\\ \\\\ "));! // the Hello the Java, the Hello Me \\\\\\\\ ???? 
        System.out.println (a.replace (" \\\\ "," \\\\ \\\\ "));! // the Hello the Java, the Hello Me \\\\ ???? 
        // string .replaceAll (string regex, string Replacement), 
        // where, regex regular expression, replacement representation the replacement string 
        // what is positive expression 
        //? ? ? ? 


        // string comparison: 
        // Method 1 equals () methods 
        String Str0 = "ABC"; 
        String str02 = new new String ( "ABC"); 
        String str03 = "the ABC"; 
        System.out.println (str0.equals ( str02)); // to true 
        System. 
        System.out.println (str0.equalsIgnoreCase (str03)); // to true
        action and syntax equals // equalsIgnoreCase () method () method of the same, the only difference is equalsIgnoreCase () is case-insensitive comparison 
        @ Method 2: == 
        System.out.println (Str0 == str02); to false // 
        // equals () method and is == operator performs two different operations 
        // equals () methods in the comparison character string object. == operator to compare the two object reference to see if they refer to the same instance. 
        // instance variables s1 pointer to the string by the "abc" created. within the meaning of the object s2 is initialized as abc created. 
        // string object content so that the two are the same. But they are different objects, which means that s1 and s2 does not point to the same object, so they are not a ==. 

        @ Method 3: compareTo (); ordering frequently applied to 
        // the compareTo () method is used to compare two strings lexicographically size, the comparison is based on the Unicode values of the respective characters. 
        otherstr //str.compareTo(String); 
        // character sequence it lexicographically to the character sequence represented otherstr str parameter representation for comparison. 
        // If lexicographically before str located otherster parameters, the comparison result is a negative integer;
        If after // str located otherstr, the comparison result is a positive integer; 
        // if the two strings are equal, the result is 0 
        System.out.println (str0.compareTo (str03)); 32 // 
        System.out.println (str03.compareTo (Str0)); // - 32 
        System.out.println (str0.compareTo (str02)); // 0 

        ASCII // a is 65, a is an ASCII 97 

        // string traversal 
        string = YY "you are you, Not Me!"; 
        for (int I = 0; I <yy.length (); I ++) { 
            char TEMP = yy.charAt (I); 
            of System.out.print (TEMP); / / you are you, Not Me! 
        } 
        System.out.println (); 
        char CH [] = yy.toCharArray ();  
        for (int i = 0; i <ch.length; i ++) {
            of System.out.print (CH [I]); // you are you, Not Me! 
        }
 
        // Find the string: 
        index // indexOf () method returns the character (string) the first occurrence of the specified string position, if found, the index value is returned, otherwise -1. 
        //str.indexOf(value) 
        //str.indexOf(value,int fromIndex) 
        // start from scratch, or specify the location to find 
        // wherein, str represents the specified string; value representing the character to be searched (the string); fromIndex A lookup when the starting index, 
        // fromIndex If not specified, the default start searching at the start position of the specified string (i.e. fromIndex defaults to 0). 
        You = String "How Great are you?"; 
        Int index0 = you.indexOf ( "ou"); // search string 
        int index1 = you.indexOf ( 'a' ); // search character 
        int index2 = you.indexOf ( 'a', 10); // start searching the search range includes the character position 
        System.out.println (index0); // 15, o index value 
        System.out.println (index1); // 7
        System.out.println (index2); // 10 
        // check if a string is empty 
        // lastIndexOf () method returns the index of the last occurrence of a specified string of characters (strings), can be found if the index value is returned, otherwise -1.
        //str.lastIndexOf(value) 
        //str.lastlndexOf(value, int fromIndex) 
        // Note: The search strategy lastIndexOf () method is from right to left to find, if you do not specify the starting index, the default from the string Find the end. 
        System.out.println (you.lastIndexOf ( "OU")); 15 // 
        System.out.println (you.lastIndexOf ( 'A')); 10 // 
        System.out.println (you.lastIndexOf ( 'A ', 9)); // 7, 9 from the forward search range, because it is from the end of the search 

        methods can be found in the class of characters charAt // string () specified in the index string 
        // string name. charAt (index value) 
        System.out.println (you.charAt (0)); // H 

        // empty string 
        // "" is the length of a string of 0 and a duty of memory is allocated a memory space the method may be used Object object. 
        // null null reference value indicating an object's memory is not allocated, the null method invocation string throws a null pointer exception 
        IF (oo.length () == 0) {
        OO = String ""; 
            System.out.println ( "empty string"); // empty string 
        } 
        IF (oo.equals ( "")) { 
            System.out.println ( "Also the this IS Kong"); Also the this IS Kong // 
        } 
        String QQ = null; 
        IF (QQ == null) { 
            System.out.println ( "IS null the this String"); // null the this IS String 
        } 

        String = STr11 new new String (); 
        String = null str22; 
        string str33 = ""; 

        length; System.out.println (str33.length ()) // empty string "" is 0 
        //System.out.println (str22.length ()); / / throws null pointer exception 
        // Exception in thread "main" java.lang .NullPointerException

        System.out.println(str11); // 输出"" 
        System.out.println (str11 == str22); // compare memory address, return false
        System.out.println (str11.equals (str22)); // the comparison value, the process returns to false 
        System.out.println (str22 == str33); // comparing the memory address returns to false 
        System.out.println (str33 .equals (str22)); // the comparison value, the process returns to false 
        System.out.println (STr11 == str33); // comparing the memory address returns to false 
        System.out.println (str11.equals (str33)); // comparison value, returns to true 








    } 
}

 

Reference: http://c.biancheng.net/view/5823.html

Published 45 original articles · won praise 8 · views 5865

Guess you like

Origin blog.csdn.net/wenyunick/article/details/103376913