Java 字符串的基本操作
08
09 月

Java 字符串的基本操作

来源:Thinking in Java 4th edition

以下是String的一些基本操作:

Method Arguments, Overloading Use
Constructor Overloaded: default, String, StringBuilder, StringBuffer, char arrays, byte arrays. Creating String objects.
length( ) Number of characters in the

String.

charAt( ) int Index The char at a location in the

String.

getChars( ), getBytes( ) The beginning and end from which to copy, the array to copy into, an index into the destination array. Copy chars or bytes into an external array.
toCharArray( ) Produces a char[] containing the characters in the String.
equals( ), equals- IgnoreCase( ) A String to compare with. An equality check on the contents of the two Strings.
compareTo( ) A String to compare with. Result is negative, zero, or positive depending on the lexicographical ordering of the String and the argument. Uppercase and lowercase are not equal!
contains( ) A CharSequence to search for. Result is true if the argument is contained in the String.
contentEquals( ) A CharSequence or StringBuffer to compare to. Result is true if there’s an exact match with the argument.
equalsIgnoreCase( ) A String to compare with. Result is true if the contents are equal, ignoring case.
regionMatches( ) Offset into this String, the other String and its offset and length to compare.

Overload adds “ignore case.”

boolean result indicates whether the region matches.
startsWith( ) String that it might start with. Overload adds offset into argument. boolean result indicates whether the String starts with the argument.
endsWith( ) String that might be a suffix of this String. boolean result indicates whether the argument is a suffix.
indexOf( ), lastIndexOf( ) Overloaded: char, char and starting index, String, Returns -1 if the argument is not found within this String; otherwise, returns