Common operations of the string

1. The comparison between the string can be used equals or compareTo. Equals high efficiency.

The results equals returns a Boolean value

str1.equals (str2) (together with, and if ... else)

compareTo coding demand values ​​in accordance with a difference, compared to the difference, the result returned is of type int. Equal, the result is zero, not equal, return difference

str1.compareTo(str2);

If the comparison value (str1 or str2) is not a string, error.

 

2. string interception

substring parenthesis string start and end index, the package does not include the left and right.

str1.substring (2); equals 2, taken from the start index, (i.e., starting from c) until the last.

str1.substring (1,2); equals 1, taken from the start index, the index is equal to the end 2 (without subscript is equal to 2).

 

 

3. Cutting strings

The result is an array of split

str1.split ( ","); the content in parentheses cutting
str1.split ( ",", 2) ; the content in parentheses cut, cut into two, when the number = 0 or greater than the maximum number of cutting, according to all carried out.

 

 

 

Guess you like

Origin www.cnblogs.com/lixiaojing1/p/12618961.html