Dark Horse programmer--java Learning Note Seven (String class)

Source: Internet
Author: User

1, the string class will not be changed once it is initialized.
Strings cannot be changed, and compilation allows strings to be shared. The compiler stores a variety of strings in a common storage pool. The string variable points to the appropriate location in the storage pool. There are actually only string constants. The + (except for the constant addition) and other methods of operation are not shared.
string S1 = "abc", String s2 = "abc" (they exist in the public pool), string s3 = new String ("abc"), exists in the heap; then s1== S2; s1! = S3;
2, String common functions:
1, Get:
1.1 Gets the number (length) of characters in the string.
int length ();
1.2 Gets the character based on the position.
char charAt (int index);
1.3 Gets the position of the first occurrence in the string, based on the character.
int indexOf (int ch)
int indexOf (int ch,int fromIndex): Find the first occurrence of CH from the specified position
int indexOf (String str);
int indexOf (String str,int fromIndex);
Gets the position of the first occurrence in the string, based on the string.
int lastIndexOf (int ch)
int lastIndexOf (int ch,int fromIndex): Find the first occurrence of CH from the specified position
int lastIndexOf (String str);
int lastIndexOf (String str,int fromIndex);
1.4 Gets a part of the string in the string. Also called a substring.
String substring (int beginindex, int endIndex)//contains begin does not contain end.
String substring (int beginindex);
2, convert.
2.1 Changing a string into a string array (cutting the string)
String[] Split (String regex): A regular expression is involved.
2.2 Converts a string into a character array.
Char[] ToCharArray ();
2.3 Converts a string into a byte array.
Byte[] GetBytes ();
2.4 Converts the letters in the string to case.
String toUpperCase (): Uppercase
String tolowercase (): lowercase
2.5 Replacing the contents of a string
String replace (char Oldch,char newch);
String Replace (string s1,string s2);
2.6 Remove the spaces at both ends of the string.
String trim ();
2.7 Connect the string.
String concat (String);
3, judging
3.1 Two strings are the contents of the same?
Boolean equals (Object obj);
Boolean equalsignorecase (String str), ignoring uppercase comparison string contents.
3.2 Does the string contain the specified string?
Boolean contains (string str);
3.3 Whether the string begins with the specified string. Whether to end with the specified string.
Boolean StartsWith (String);
Boolean EndsWith (String);
4, Comparison.
int compare (String s) returns-1 0 1
5, sometimes it takes a shorter string to build a string, the string connection is inefficient, and a new string object is constructed each time the connection string is made. Time consuming and wasted space.
Similar problems can be avoided by using the Stringbulider class. StringBuffer is a thread-safe class that reduces efficiency.
Main methods:
1, add:
StringBuffer append (data);
StringBuffer Insert (Index,data);
2, Delete:
StringBuffer Delete (Start,end): Contains the header, does not contain the tail.
StringBuffer deletecharat (int index): Deletes the element at the specified position
3, find:
Char charAt (index);
int indexOf (string);
int lastIndexOf (string);
4, modify:
StringBuffer replace (start,end,string);
void Setcharat (Index,char);
Additions and deletions Change C (Create) U (update) R (read) D (delete)
Reverse () reverse traverse tritosize () changes size.
6, conversion between wrapper class and string
Automatic Packing: Automatic unpacking
Note: Automatic boxing, if the boxed is a byte, then the data will be shared will not re-open space.
Basic Type---> string
1, Basic type value + ""
2, valueof (basic type value) with static method in string class;
3, using the static method of integer Valueo (basic type value);
String---> Basic type
1, use the static method in the wrapper class XXX parsexxx ("type xxx string");
int parseint ("intstring");
Long Parselong ("longstring");
Boolean Parseboolean ("booleanstring");
Only character has no parse method.
2, if the string is encapsulated by an integer object.
Another non-static method can be used, intvalue ();
Converts an integer object to a base data type value.
Decimal--and other binary.
Tobinarystring
Tooctalstring
Tohexstring
Other binary---decimal.
parseint ("string", radix)

Dark Horse programmer--java Learning Note Seven (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.