Common methods in the String class and common methods in the String class

Source: Internet
Author: User

Common methods in the String class and common methods in the String class

There are many methods and constructor methods in the String class: 1. Common constructor: 1. public String () String str = new String ("abcdefg ") 2. public String (byte [] bytes): constructs the byte [] B = {'A', 'B', 'C', 'D'} String object through the byte array '}; string t1 = new String (B); System. out. println (t1); output result: abcd 3, public String (byte [] bytes, int offset, int length): bytes-bytes to be decoded as characters, offset-index of the first byte to be decoded, length-number of bytes to be decoded byte [] B = {'A', 'B', 'C ', 'D'}; String t3 = new String (B, 1, 2); System. ou T. println (t3); output result: bc 4, public String (char [] value): the char [] c = {'0', '1 ', '2', '3'}; String t4 = new String (c); System. out. println (t4); output result: 0123 5. public String (char [] value, int offset, int count): value-as the array of the character source, offset-initial offset, count-length char [] c = {'0', '1', '2', '3'}; String t5 = new String (c, 1, 3); System. out. println (t5); output result: 123 2. Common method: Take String object String str = "abcdefg" String str1 = "bcdefg" as an Example 1, Public char charAt (int index): returns the char value at the specified index. The index range is from 0 to length ()-1. Char str2 = str. charAt (2); System. out. println (str2); output result: c 2, public int codePointAt (int index): returns the characters at the specified index (Unicode code point ). The index references the char value (Unicode code unit), which ranges from 0 to length ()-1. Int t = str. codePointAt (2); System. out. println (t); output result: 99 3. public int codePointBefore (int index): returns the character (Unicode Code Point) before the specified index, which ranges from 1 to length. Int t1 = str. codePointBefore (2); System. out. println (t1); output result: 98 4. public int codePointCount (int beginIndex, int endIndex): returns the Unicode code points in the specified text range of this String, the text range starts from the specified beginIndex to endIndex-1. Int t2 = str. codePointCount (0, 2); System. out. println (t2); output result: 2 5. public int compareTo (String anotherString): Compares two strings in alphabetical order, which is based on the Unicode values of each character in the String. Int t3 = str. compareTo (str1); System. out. println (t3); output result:-1 6, public int compareToIgnoreCase (String str): Compares two strings in alphabetical order, case-insensitive. 7. public String concat (String str): connect the specified String to the end of the String. If the length of the parameter String is 0, this String object is returned. String str3 = str. concat (str1); System. out. println (str3); output result: abcdefgbcdefg 8, public boolean contains (CharSequence s): returns true if and only if the string contains the specified char value sequence, however, it must be continuous and case sensitive. Boolean str4 = str. contains (str1); System. out. println (str4); output result: true 9, public boolean contentEquals (CharSequence cs): Compare this string with the specified CharSequence. True is returned only when this String and the specified sequence represent the same char value sequence. Boolean s = str. contentEquals (str1); System. out. println (s); output result: false 10, public boolean endsWith (String suffix): test whether the String ends with the specified suffix. If the parameter is a null String, or equal to this String object, the result is true. Boolean t = str. endsWith (str1); System. out. println (t); output result: true 11, public boolean equals (Object anObject): Compare this string with the specified Object. True is returned only when this parameter is not null and is a String object that represents the same character sequence as this object. Boolean t1 = str. equals (str1); System. out. println (t1); output result: false 12, boolean inclusignorecase (String anotherString): Compare this String with another String, case-insensitive. 13. static String format (String format, Object... args): return a formatted String using the specified format String and parameters. String s1 = str. format ("% e", 2.22); System. out. println (s1); output result: 2.220000e + 00 14, public byte [] getBytes (): the default Character Set encodes this String into a byte sequence, store the result to a new byte array, and obtain the byte array. Byte [] bytes = str. getBytes (); System. out. println (bytes); output result: [B @ 1ac3379 15, public void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin ): the first character to be copied is at srcBegin; the last character to be copied is at the srcEnd-1. To be copied to the dst subset group, starting from dstBegin, char t [] = {'k', 'l', 'M', 'n', 'O ', 'P'}; str. getChars (1, 3, t, 1); System. out. println (t); output result: kbcnop 16, public int hashCode (): return the hash code of this string. Int x = str. hashCode (); System. out. println (x); output result:-1206291356 17. int indexOf (int ch): returns the index of the first occurrence of the specified character in this string, and returns the index of the first occurrence of the character. Int x1 = str. indexOf (99); System. out. println (x1); output result: 2 18, public int indexOf (int ch, int fromIndex): returns the index of the specified character for the first time in this string, start from the specified index. Int x2 = str. indexOf (99, 1); System. out. println (x2); output result: 2 19, public int indexOf (String str): returns the index of the first occurrence of the substring in this String. 20. public int indexOf (String str, int fromIndex): returns the index of the first occurrence of the substring from the specified index. 21. public boolean isEmpty (): returns true only when length () is 0. 22. public int lastIndexOf (int ch): returns the index of the last occurrence of the specified character in this string. 23. public int lastIndexOf (int ch, int fromIndex): returns the index of the last occurrence of the specified character in this string, and starts reverse search from the specified index. 24. public int lastIndexOf (String str): returns the index at the rightmost of the substring. 25. public int lastIndexOf (String st, int fromIndex): returns the index of the last occurrence of the specified substring in this String and starts reverse search from the specified index. 26. public int length (): returns the length of this string. 27. public String replace (char oldChar, char newChar): returns a new String, which is obtained by replacing all the oldChar in this String with newChar. String s3 = str. replace (str, str1); System. out. println (s3); output result: bcdefg 28, public boolean startsWith (String prefix): test whether the String starts with the specified prefix. If the parameter is a null String, or equal to this String object, true is returned. Boolean s5 = str. startsWith (str1); System. out. println (s5); output result: false 29, public boolean startsWith (String prefix, int toffset): test whether the substring starting from the specified index starts with the specified prefix. Boolean s6 = str. startsWith (str1, 1); System. out. println (s6); output result: true 30, public String substring (int beginIndex): returns a new String, which is a substring of this String. The substring starts from the character at the specified index until the end of the string. String s7 = str. substring (5); System. out. println (s7); output result: fg 31, public String substring (int beginIndex, int endIndex): returns a new String, which is a substring of this String. The substring starts at beginIndex and ends at endIndex-1. 32. public String toLowerCase (): converts all characters in this String to lowercase. String str = "ABCD"; String s9 = str. toLowerCase (); System. out. println (s9); output result: abcd 33, public String toUpperCase (): converts all characters in this String to uppercase. 34. public String trim (): This String removes leading and trailing blank copies. If there is no leading or trailing blank, this String is returned. String str = "abc" String a2 = str. trim (); System. out. println (a2); output: abc 1

[New Technology] The most popular java background framework combination java springmvc mybaits mysql oracle html5 background framework source codeObtain 【] 

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.