Java action String, which focuses on recording the operation of a string using ReplaceAll

Source: Internet
Author: User

String Lookup

The string provides two ways to find a string, namely the IndexOf and LastIndexOf methods.

1, indexOf (String s)

The method is used to return the index position of the first occurrence of the argument string s in the specified string, and when the IndexOf () method of the string is called, the position of S is searched from the beginning of the current string, and if the string s is not retrieved, the method returns-1

1 String str = "We are students"; 2 int size = Str.indexof ("a"); The value of the variable size is 3

2, LastIndexOf (String str)

The method is used to return the index position of the last occurrence of the string. When the LastIndexOf () method of the string is called, the parameter string str is retrieved from the beginning of the current string and returned at the last index where STR occurred. If the string str is not retrieved, the method returns-1.

If the argument in the LastIndexOf method is an empty string "", the returned result is the same as the return result of the length method.

Gets the character at the specified index position

Use the Charat () method to return the characters at the specified index.

1 String str = "Hello word"; 2 char MyChar =  Str.charat (5);  The result of MyChar is W
Get substring

The string can be intercepted by the substring () method of the String class. What these methods have in common is that they are intercepted using the subscript of the string, and that the string subscript should be clear starting from 0. Occupies an index position in a hollow cell of a string.

1, substring (int beginindex)

The method returns a substring that begins at the specified index position to intercept the end of the string.

1 string str = "Hello word"; 2 string substr = Str.substring (3); Gets the string at which the substr value is Lo Word

2, substring (int beginindex, int endIndex)

Beginindex: Starting to intercept the index position of the substring

EndIndex: The end position of the substring within the entire string

1 string str = "Hello word"; 2 string substr = Str.substring (0,3); The value of substr is Hel
Remove spaces

The trim () method returns a copy of the string, ignoring leading and trailing spaces.

String substitution

The Replace () method allows you to replace the specified character or string with a new character or string

OldChar: The character or string to replace

Newchar: Used to replace the contents of the original string

The replace () method replaces all Oldchar with Newchar if the character Oldchar to be replaced appears repeatedly in the string. It is important to note that the case of the character Oldchar to be replaced is consistent with the case of the characters in the original string.

1 string str= "Address"; 2 string newstr = Str.replace ("A", "a");//NEWSTR value is address
Judging the start and end of a string

The StartsWith () method and the EndsWith () method are used to determine whether a string starts or ends with the specified content, respectively. The return values for both methods are of type Boolean.

1, StartsWith (String prefix)

This method is used to determine whether the prefix of the current string object is a string specified by the parameter.

2, EndsWith (String suffix)

This method is used to determine whether the current string ends with the given substring

Determine if strings are equal

1, Equals (String otherstr)

If two strings have the same character and length, the Equals () method is used to return true when compared. The Equals () method is also case-sensitive when compared.

2, Equalsignorecase (String otherstr)

The Equalsignorecase () method is associated with the Equals () type, but the case is ignored when comparing.

Compare two strings in a dictionary order

The CompareTo () method compares two strings in a dictionary order, which is based on the Unicode values of the individual characters in the string, and compares the sequence of characters represented by this string object in dictionary order with the sequence of characters represented by the parameter string. If this string object is in the dictionary order before the parameter string, the result of the comparison is a negative integer, and if the string object is in the dictionary order after the parameter string, the comparison result is a positive integer, and if the two strings are equal, the result is 0.

1 Str.compareto (String otherstr);
Letter Case Conversion

The toLowerCase () method of the string overwrites all characters in the string from uppercase to lowercase, while the tuuppercase () method rewrites lowercase letters in the string to uppercase.

1 str.tolowercase (); 2 str.touppercase ();
String segmentation

Use the split () method to split the contents of the string by a specified delimiter character or string, and to store the split result in a character array.

1 str.split (String sign);

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

There is no uniform symbol for dividing a string, and if you want to define multiple delimiters, you can use the symbol ' | '. For example, ", |=" means that the delimiters are "," and "=", respectively.

1 str.split (String sign, in limit);

The method splits the string according to the given delimiter and limits the number of splits.

The

Java action String, which focuses on recording the operation of a string using ReplaceAll

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.