Operation of a Java string string

Source: Internet
Author: User
Tags first string

In Java, the string class contains more than 50 methods to implement various operations of a string. A string variable must be initialized before it can be used, and if the declaration is assigned a value of NULL, the value of the declared string is null, otherwise it represents an uninitialized state. 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, and the connection string can also use the + sign. You can concatenate different data types and automatically call the ToString () method, as long as one operand of the "+" operator is a string, the compiler will turn the other operand into a string.


(2) 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 string.


(3) 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) string comparison
      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, similar to the CompareTo method provided by the character class, and the CompareTo method provided by the character class compares two character class data , and the string data is compared here.
      The comparison process is actually the result of a comparison of characters in the same position in two strings, sorted in Unicode. If no difference is found throughout the comparison, two strings are exactly equal, The CompareTo method returns 0; If a different place is found during the comparison process, the comparison process will stop, at which point it must be that the two string is not the same at a certain position, if the character of the current string at that 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 to the argument string and returns True when two strings are equal. Otherwise, false is returned.
      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.

When you compare strings, you cannot use "= =", which is used to compare addresses of two strings.


(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 a specified string, taking advantage of 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.


(12) Segmentation of strings

1) Split (String sign)

Sign is a delimiter for splitting a string, or you can use regular expressions

2) Split (String sign, int limit)

Limit is used to limit the number of splits


(13) string variable and StringBuffer class
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)

Operation of a Java string string

Related Article

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.