Summary of String Operations

Source: Internet
Author: User
Tags time and date

String manipulation
First, get the string length
1, String. Length ()
2, String. LastIndexOf ("")
* Note: lastIndexOf ("") in the argument with a space, which returns the length of the string

Second, get the specified position character
1, String. charAt (int index)
* Note: I, the string empty lattice occupies an index position
II, string is an array, so the index position is the array pin

Third, intercept the string
1, String. substring (int beginindex,endindex);
* Note: This function means: from the position beginindex start to endindex end.
* If the function is represented as: string. substring (int beginindex) indicates
Start from begin to end.

Iv. removing spaces at the beginning and end of a string
1, String. Trim ();


V. Searching for strings
1. String. IndexOf ("string to find") = Returns the index of the position where the string is located
* Note: Find the index position for the first occurrence of the string
2. String. LastIndexOf ("string to find")
* Note: Find the location where the result of the last index of the string appears

Six, string substitution
1, String. Replace ("OldChar", "Newchar");
* Note: Oldchar is the character that needs to be replaced, Newchar is used to replace the string

Determines whether the beginning or end of a string is a specified string
1, StartsWith (string prefix) This method is used to determine whether a string prefix
Beginning.
2, EndsWith (string suffix) This method is used to determine whether a string suffix
End.

Eight, determine whether the string is equal
The comparison operator "= =" compares the memory address with the same, and equals () compares the string
Whether it has the same character and length.
1, judge whether the string is equal with Equals () or equalsignorecase (), can not be used simply
"= =" to compare, because the string object is a reference object, so even if the string content is the same
The address is not necessarily the same.
* Note: For example the comparison string str and the string other syntax are Str.equals (other).
Note: The difference between the two equals () is case-sensitive, while the equalsignorecase ()
Case insensitive.

Comparison of Strings by dictionary order: CompareTo ().
Note: Str.compare (ST), if the Str dictionary is in front of St, the result returns a negative number, if Str
After St, an integer is returned, and only 0 is returned if Str.equals (St) is true.

Ten, letter-case conversion
1. toLowerCase () changes the uppercase letters in the original string to lowercase, touppercase () the original string
lowercase to uppercase. For example: Str.tolowercase () changes the capitalization of a string to lowercase.

Xi. Segmentation of strings
1. Split ("separator")
* Note: The delimiter is a character contained in a string, the result of the split is an array, so the split
The result needs to be stored in an array.
2, Split ("separator", divided into the number of copies)

12. Time and date formatting

13. Regular Expressions

14. String Generator


Summary of String Operations

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.