Use of the String class

Source: Internet
Author: User
Tags array to string

  1. Common construction methods
    1. Public String (): Empty construct
    2. public string (byte[] bytes): Convert byte array to string
    3. public string (byte[] bytes,int index,int length): Turns a portion of a byte array into a string
    4. public string (char[] value): Convert character array to string
    5. public string (char[] Value,int index,int count): Turns a part of a character array into a string
    6. public string (string original): Convert string constant value to string
           
          
      1. char[] c={‘a‘,‘b‘,‘c‘};
      2. Strintg s=new String(c);
      3. //最后输出的结果是abc
      4. //通过构造方法产生字符串
  2. Common face questions for string class
    1. Determines whether S1 and S2 defined as string are equal?
           
          
      1. String s1 = "abc";
      2. String s2 = "abc";
      3. System.out.println(s1 == s2);
      4. System.out.println(s1.equals(s2));
      The results are equal, because S1 and S2 are created in the set value, and are created in the constant pool, so their comparisons are equal
    2. The following sentence creates several objects in memory?
        
            
          
      1. String s1 = new String("abc");
      2 objects are created, one is an Strig type object in heap memory, and the other is an object of ABC in a constant pool.
    3. Determines whether S1 and S2 defined as string are equal?
           
          
      1. String s1 = new String("abc");
      2. String s2 = "abc";
      3. System.out.println(s1 == s2);
      4. System.out.println(s1.equals(s2));
      S1==S2 is not equal because S1 is equivalent to an object created in memory, and S2 is an object in a constant pool, and the address value of the 2 is different. But equals compares the property value of the object, and the result is true.
    4. Determines whether S1 and S2 defined as string are equal?
           
          
      1. String s1 = "a" + "b" + "c";
      2. String s2 = "abc";
      3. System.out.println(s1 == s2);
      4. System.out.println(s1.equals(s2));
      The result is true because there is a constant automatic optimization mechanism in Java, which is a constant value when creating the object, so Java will automatically perform the operation and produce a new constant value, so the two comparisons are the same constants.
    5. Determines whether S1 and S2 defined as string are equal?
           
          
      1. String s1 = "ab";
      2. String s2 = "abc";
      3. String s3 = s1 + "c";
      4. System.out.println(s3 == s2);
      5. System.out.println(s3.equals(s2));
      The s3==s2 result is false, although the S1 and S2 constants, but S3 adds the S1 variable when the operation, because the characterstring concatenation is done byStringBuilder(orStringBuffer) class and itsappendmethod is implemented. So there is a new character buffer in the heap memory of the object, so the comparison between the address value is definitely different.
  3. The judging function of the string class
      1. Boolean equals (Object obj): Compares the contents of strings for the same, case-sensitive
      2. Boolean equalsignorecase (String str): Compares whether the contents of a string are the same, ignoring case
      3. Boolean contains (String str): Determines whether a large string contains a small string
      4. Boolean startsWith (String str): Determines whether a string begins with a specified string
      5. Boolean endsWith (String str): Determines whether a string ends with a specified string
      6. Boolean IsEmpty (): Determines whether the string is empty.
  4. Get function of String class
      1. int length (): Gets the length of the string.
      2. char charAt (int index): Gets the character at the specified index position
      3. int indexOf (int ch): Returns the index at the first occurrence of the specified character in this string.
      4. int indexOf (String str): Returns the index of the first occurrence of the specified string in this string.
      5. int indexOf (int ch,int fromIndex): Returns the index at the first occurrence of the specified character from the specified position in this string.
      6. int indexOf (String str,int fromIndex): Returns the index at the first occurrence of the specified string from the specified position in this string.
      7. LastIndexOf: Reverse lookup. The effect is the same as indexof.
      8. string substring (int start): Intercepts the string starting at the specified position, at the end of the default.
      9. string substring (int start,int end): Intercepts the string from the specified position to the specified position.
  5. Conversion capabilities of the String class
      1. Byte[] GetBytes (): Converts the string to a byte array.
      2. Char[] ToCharArray (): Converts a string to a character array.
      3. static string ValueOf (char[] CHS): Turns the character array into a string.
      4. static string valueOf (int i): turns data of type int into a string.
        Note: The valueof method of the string class can turn any type of data into a string.
      5. String toLowerCase (): Turns the string into lowercase. Know
      6. String toUpperCase (): Turns the string into uppercase.
      7. String concat (String str): Concatenation of strings.
  6. Additional features of the String class
      1. The Replace function of string
        1. String replace (char Old,char new)//use to replace the specified character
        2. String Replace (string old,string New)//replaces the specified string
      2. String removal of the leading and trailing spaces
        1. String trim ()//To remove the space between the end and end of the string and return the new string.
      3. string comparison of two string sizes in dictionary order
        1. int CompareTo (string str)//By Code table compares the size of two strings in the Code table (Unicode code table), returns a negative number, 0, a positive integer
        2. int comparetoignorecase (String str)//Effect ditto, except that the method does not calculate case.


From for notes (Wiz)

Use of the String class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.