Summary of Java String Operations

Source: Internet
Author: User
Tags first string

Common operations and methods of the String class object
In Java, the string class contains more than 50 methods to implement the various operations of strings, and some of the methods we need to use frequently are described below.
(1) Connection of strings
public string concat (String str)
The parameter of the method is a string class object that connects the string str in the argument to the back of the original string.
(2) To find the length of the string
public int Length ()
Returns the length of the string, where the length refers to the number of Unicode characters in the strings.
(3) to find a character in a position in a string
public char charAt (int index)
The method indexes a string at a particular location to get the character at the specified position in the string. It is worth noting that the index of the first character in the string is 0, the second character's index is 1, and so on, and the last character's index is length ()-1.
(4) Comparison of strings
The comparison string can take advantage of the following methods provided by the String class:
1) public int compareTo (String anotherstring)
The method compares two strings, similar to the CompareTo method provided by the character class, and the CompareTo method provided by the character class compares two character class data, compared with string data.
The comparison process is actually the result of a comparison of characters in the same position in the two strings, sorted in Unicode. If no difference is found throughout the comparison, two strings are exactly equal, and the CompareTo method returns 0; Found a different place, then the comparison process will stop, it must be two strings in a certain position is not the same, if the current string in this position is greater than the character at this position in the argument, the CompareTo method returns an integer greater than 0, otherwise returns an integer less than 0.
2) Public boolean equals (Object anobject)
This method compares two strings, similar to the Equals method provided by the character class, because they are all methods of overloading the object class. This method compares the current string and the argument string, returns True when two strings are equal, or returns false.
3) Public boolean equalsignorecase (String anotherstring)
This method is similar to the Equals method, except that the Equalsignorecase method ignores the difference in the case of letters.
(5) Extracting substrings from a string
Using the substring method provided by the string class, you can extract a substring from a large string, which has two common forms:
1) public String substring (int beginindex)
The method removes the remaining characters from the current string as a new string from the Beginindex position.
2) public String substring (int beginindex, int endIndex)
The method takes a substring from the current string, ending with a substring from the beginindex position to endIndex-1. The length of the substring return is endindex-beginindex.
(6) Determining the prefix and suffix of a string
Determines whether the prefix of a string is the specified string using the following methods provided by the String class:
1) Public boolean startsWith (String prefix)
This method is used to determine whether the prefix of the current string is prefix consistent with the string specified in the argument, and if so, returns True, otherwise false.
2) Public boolean startsWith (String prefix, int toffset)
This method is used to determine whether the prefix of a substring starting at the Toffset position of the current string is prefix consistent with the string specified in the argument, and if so, returns True, otherwise false.
Determines whether the suffix of a string is a specified string using the method provided by the String class:
public boolean endsWith (String suffix)
This method is used to determine whether the suffix of the current string is suffix consistent with the string specified in the argument, and if so, returns True, otherwise false.
(7) Finding a single character in a string
The lookup of a single character in a string can take advantage of the following methods provided by the String class:
1) public int indexOf (int ch)
This method is used to find the position of a particular character ch in the current string. This method looks from the back, if the character ch is found in the string, the first occurrence of the character ch in the string, or 1 if the character ch is not found in the entire string.
2) public int indexOf (int ch, int fromIndex)
This method is similar to the first method, except that the method looks backwards from the Fromindex position and returns the character CH where the string first appears.
3) public int lastIndexOf (int ch)
This method is similar to the first method, except that the method looks forward from the end of the string, and still returns the position of the character ch at the first occurrence of the string.
4) public int lastIndexOf (int ch, int fromIndex)
This method is similar to the second method, except that the method looks forward from the Fromindex position, and the return is still the position of the character ch at the first occurrence of the string.
(8) Finding of string neutron strings
The lookup of a string substring is very similar to finding a single character in a string, and you can take advantage of the following methods provided by the String class:
1) public int indexOf (String str)
2) public int indexOf (String str, int fromIndex)
3) public int lastIndexOf (String str)
4) public int lastIndexOf (String str, int fromIndex)
(9) Conversion of character case in string
A character-case conversion in a string can take advantage of the following methods provided by the String class:
1) Public String toLowerCase ()
This method converts all characters in a string to lowercase and returns a new string after the conversion.
2) Public String toUpperCase ()
This method converts all characters in a string to uppercase and returns a new string after the conversion.
(10) Removal of extra spaces in the string
Public String trim ()
This method simply removes the opening and closing spaces and returns the resulting new string. It is important to note that the spaces in the middle of the original string are not removed.
(11) Substitution of characters in a string
1) Public String replace (char Oldchar,char Newchar)
The method replaces all characters in the current string with the character Newchar Oldchar, and returns a new string.
2) public string Replacefirst (string regex, string replacement)
The method substitutes the contents of the string replacement for the first string encountered in the current string, and the substring that matches the regex, and returns the resulting new string.
3) public string ReplaceAll (string regex, string replacement)
This method replaces all strings encountered in the current string with the string regex consistent with the string replacement, and returns the resulting new string.
String variables and StringBuffer classes
1. Creating a StringBuffer Class object
The StringBuffer class object represents a string variable, and each StringBuffer class object is a string variable that can be expanded and modified. The following are common StringBuffer class constructors:
(1) public stringbuffer ()
(2) public stringbuffer (int length)
(3) Public stringbuffer (String str)

Summary of Java 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.