Summary of operations for strings in Java

Source: Internet
Author: User
Common operations and methods of 2.String class objects
In Java, the string class contains more than 50 methods to implement the various operations of strings, and here are some of the methods we need to use frequently.
(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 a string, where the length refers to the number of Unicode characters in the strings.
(3) to find the character of a position in a string
public char charAt (int index)
The method indexes a string at a specific location to get the character at the specified position in the string. It is noteworthy that the index of the first character in the string is 0, the second character is 1, and so on, the last character is indexed to length ()-1.
(4) Comparison of strings
Comparison strings can take advantage of the following methods provided by the String class:
1) public int compareTo (String anotherstring)
This method compares two strings and is 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 comparing characters in the same position in the two string in the order of Unicode. If no differences are found throughout the comparison, the two strings are completely equal, and the CompareTo method returns 0; Found a different place, then the comparison will stop, and it must be two strings that are not the same at some point, and if the characters at this position in the current string are greater than the characters in this position in the argument, the CompareTo method returns an integer greater than 0, otherwise a whole number less than 0 is returned.
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 that overload the object class. This method compares the current string and the parameter string, returns True when two strings are equal, or false.
3) Public boolean equalsignorecase (String anotherstring)
This method is similar to the Equals method, except that the Equalsignorecase method ignores the difference between uppercase and lowercase letters.
(5) extracting substrings from strings
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 returns the remaining characters from the current string as a new string from the Beginindex position.
2 public String substring (int beginindex, int endindex)
This method extracts a substring from the current string, from the beginindex position to the endIndex-1 end. The length of the substring return is endindex-beginindex.
(6) to determine the prefix and suffix of a string
Determines whether the prefix of a string uses the following methods provided by the string class for the specified string:
1) Public boolean startswith (String prefix)
This method is used to determine whether the prefix of the current string is consistent with the string prefix specified in the parameter, and if so, return True or false.
2 public boolean startswith (String prefix, int toffset)
This method is used to determine whether the prefix of the substring starting at the Toffset position is consistent with the string prefix specified in the parameter, and returns True if it is, or false.
Determines whether the suffix of a string uses the method provided by the string class for the specified string:
public boolean endsWith (String suffix)
This method is used to determine whether the suffix of the current string is consistent with the string suffix specified in the parameter, and if so, return True or false.
(7) Lookup of a single character in a string
A 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 back from the beginning, if character ch is found in the string, and returns the position of 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 position of the character ch at the first occurrence of the string.
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 returns the position of the character ch at the first occurrence of the string.
4 public int LastIndexOf (int ch, int fromindex)
The method is similar to the second method, except that the method looks forward from the Fromindex position and returns the position of the character ch at the first occurrence of the string.
(8) Search for strings of substrings
The lookup of a string of substrings is very similar to the lookup of 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) Character capitalization conversions in strings
Character capitalization conversions in strings, you can take advantage of the following methods provided by the String class:
1) Public String toLowerCase ()
This method converts all characters in the string to lowercase and returns the converted new string.
2) Public String toUpperCase ()
This method converts all characters in a string to uppercase and returns the converted new string.
(10) Removal of extra space in string
Public String trim ()
The method simply removes the start and end spaces and returns the resulting new string. It is noteworthy that the spaces in the middle of the original string are not removed.
(11) Substitution of characters in string
1) Public String replace (char Oldchar,char Newchar)
The method replaces all character Oldchar in the current string with the character Newchar and returns a new string.
2 public string Replacefirst (string regex, string replacement)
This method replaces the first substring encountered in the current string with the string regex in the string replacement, and returns the resulting new string.
3 public string ReplaceAll (string regex, string replacement)
This method replaces all strings that are encountered in the current string with the string regex, and returns the resulting new string, using the contents of the string replacement.
6.2.2 string variable and StringBuffer class
1. Create StringBuffer class objects
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)

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.