Common methods in the String class

Source: Internet
Author: User

There are many methods and construction methods in the String class: First, Common construction methods: 1, public string () string Str=new string ("ABCDEFG") 2, public string (byte[] bytes): By byte number      The group constructs a string object byte[]b={' A ', ' B ', ' C ', ' d '};      String T1=new string (b); SYSTEM.OUT.PRINTLN (t1); Output: ABCD 3, Public String (byte[] bytes,int offset, int length): bytes-the byte,offset to decode as a character-the index of the first byte to decode, length-to decode       The number of bytes byte[]b={' A ', ' B ', ' C ', ' d '};       String T3=new string (b,1,2);   System.out.println (T3);       Output: BC 4, public string (char[] value): Constructs a String object through a char array char[]c={' 0 ', ' 1 ', ' 2 ', ' 3 '};       String T4=new string (c);   System.out.println (T4); Output: 0123 5, public String (char[] value,int offset,int count): value-as an array of word mnemonic, offset-initial offset, count-length char[]c={' 0 '       , ' 1 ', ' 2 ', ' 3 '};       String T5=new string (c,1,3); System.out.println (T5); Output: 1232, Common Method: String Object string Str= "ABCDEFG" string str1= "BCDEFG" as example 1, public char charAt (int index): return refers to The char value at the fixed index.         The index range is from 0 to length ()-1.  Char Str2=str.charat (2);     System.out.println (STR2); Output: C 2, public int codepointat (int index): Returns the character at the specified index (Unicode code point).      The index references a char value (Unicode code unit), which ranges from 0 to length ()-1.      int T=str.codepointat (2); System.out.println (t);      Output: 99 3, public int codepointbefore (int index): Returns the character (Unicode code point) before the specified index, ranging from 1 to length.          int T1=str.codepointbefore (2); SYSTEM.OUT.PRINTLN (t1); Output: 98 4, public int codepointcount (int beginindex,int endIndex): Returns the number of Unicode code points in the specified text range of this String, beginning with the specified beginindex to     EndIndex-1 Place. int T2=str.codepointcount (0,2);   System.out.println (T2);           Output: 2 5, public int compareTo (string anotherstring): Compares two strings in dictionary order, which is based on the Unicode value of each character in the string.      int T3=str.compareto (STR1); System.out.println (T3); Output:-1 6, public int comparetoignorecase (string str): Compares two strings in a dictionary order, regardless of case. 7. public string concat (String str): connects the specified string to the end of this string.     If the length of the argument string is 0, this string object is returned.     String Str3=str.concat (STR1); System.out.println (STR3); Output Result: ABCDEFGBCDEFG 8, public boolean contains (charsequences): Returns True if and only if this string contains a specified sequence of char values, as long as there is a containing, but is to be contiguous, case-sensitive.        Boolean str4=str.contains (STR1); System.out.println (STR4); Output: True 9, public boolean contentequals (Charsequence CS): Compares this string to the specified charsequence.                The result is true when and only if this String represents the same sequence of char values as the specified sequence.        Boolean s=str.contentequals (STR1); System.out.println (s);               Output: False 10, public boolean endsWith (string suffix): Tests whether this string ends with the specified suffix, or true if the argument is an empty string or equals this string object.        Boolean t=str.endswith (STR1); System.out.println (t); Output: True 11, public boolean equals (object AnObject): Compares this string to the specified object.             The result is true if and only if the parameter is not NULL and is a String object that represents the same sequence of characters as this object.       Boolean t1= str.equals (STR1); SYSTEM.OUT.PRINTLN (t1); Output: False 12, Boolean equalsignorecase (String anotherstring): Compares this string to another string, regardless of the case.        13. Static string Format (String format,object. Args): Returns a formatted string using the specified format string and parameters.        String S1=str.format ("%e", 2.22); System.out.println (S1); Output results: 2.220000e+00 14, public byte[] GetBytes (): The default character set encodes this String into a byte sequence and stores the result in a new byte array, resulting in a byte array.       Byte[] bytes= str.getbytes (); System.out.println (bytes); Output: [[email protected] 15, public void getChars (int srcbegin,int srcend,char[] dst,int dstbegin): The first character to be copied is located in the Srcbegin; The last character to be copied is located at srcEnd-1.       To copy to the DST sub-array, start at Dstbegin, Char t[]={' k ', ' l ', ' m ', ' n ', ' o ', ' P '};       Str.getchars (1,3,t,1); System.out.println (t);       Output: Kbcnop 16, public int hashcode (): Returns the hash code for this string.       int X=str.hashcode (); SYSTEM.OUT.PRINTLN (x);     Output:-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: 2 18, public int indexOf (int ch,int fromIndex): Returns the index at the first occurrence of the specified character in this string, starting the search from the specified index.        int X2=str.indexof (99, 1); System.out.println (x2); Output: 2 19, public int indexOf (string str): Returns the index where the specified substring first appears in this string. 20, public int indexOf (String str, int fromIndex): Returns the index of the first occurrence of the specified substring in this string, starting at the specified index. 21. Public Boolean IsEmpty (): When and only if length () Returns true for 0 o'clock. 22, public int lastIndexOf (int ch): Returns the index of the specified character at the last occurrence 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, starting at the specified index to reverse-search. 24, public int lastIndexOf (string str): Returns the index of the rightmost occurrence of the specified substring in this string. 25, public int lastIndexOf (String st,int FromIndex): Returns the index of the last occurrence of the specified substring in this string, starting at the specified index to reverse the search. 26, public int length (): Returns the length of this string.              27. public string Replace (char Oldchar,char Newchar): Returns a new string that is obtained by replacing all OldChar that appear in this string with Newchar.       String s3= str.replace (str, str1); System.out.println (S3);              Output: BCDEFG 28, public boolean startsWith (string prefix): Tests whether this string starts with the specified prefix, or returns True if the argument is an empty string or equals this string object.        Boolean s5=str.startswith (STR1); System.out.println (S5);        Output: False 29, public boolean StartsWith (String Prefix,int toffset): Tests whether the substring starting at the specified index begins with the specified prefix.        Boolean S6=str.startswith (STR1, 1); System.out.println (S6); Output: True 30, public string substring (int beginindex): Returns a new string that is a substring of this string.        The substring starts at the character at the specified index until the end of the string. StrinG s7=str.substring (5); System.out.println (S7); Output: FG 31, public string substring (int beginindex,int endIndex): Returns a new string that is a substring of this string. The substring starts at Beginindex until the index is 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: ABCD 33, public string toUpperCase (): Converts all characters in this string to uppercase.     34, public string trim (): This string removes the leading and trailing blanks, and returns this string if there are no leading and trailing blanks.     String str= "abc" string A2=str.trim (); SYSTEM.OUT.PRINTLN (A2);  Output Result: ABC 1

"New technology" now the most popular Java background framework combination Java SPRINGMVC mybaits mysql Oracle HTML5 backend framework Source acquisition ""

Common methods in 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.