public final class String
- Object
- String
ImplementsCharSequence, Comparable<String>
Fields
public static final Comparator<String> CASE_INSENSITIVE_ORDER |
Constructors
public String() | Initializes a newly created String object so that it represents an empty character sequence. |
public String(byte[] bytes) | Construct a new String by converting the specified array of bytes using the platform’s default character encoding. |
public String(byte[] bytes, int off, int len) | Construct a new String by converting the specified subarray of bytes using the platform’s default character encoding. |
public String(byte[] bytes, int off, int len, String enc)
throws UnsupportedEncodingException | Construct a new String by converting the specified subarray of bytes using the specified character encoding. |
public String(byte[] bytes, Charset charset)
throws UnsupportedEncodingException | Construct a new String by converting the specified array of bytes using the specified character encoding. |
public String(byte[] bytes, String enc)
throws UnsupportedEncodingException | Construct a new String by converting the specified array of bytes using the specified character encoding. |
public String(char[] value) | Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. |
public String(char[] value, int offset, int count) | Allocates a new String that contains characters from a subarray of the character array argument. |
public String(String value) | Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. |
public String(StringBuffer buffer) | Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. |
public String(StringBuilder buffer) |
Methods
public char charAt(int index) | Returns the character at the specified index. |
public int compareTo(String anotherString) | Compares two strings lexicographically. |
public int compareToIgnoreCase(String anotherString) | |
public boolean contentEquals(CharSequence cs) | |
public boolean contentEquals(StringBuffer sb) | |
public static String copyValueOf(char[] data) | |
public static String copyValueOf(char[] data, int offset, int count) | |
public String concat(String str) | Concatenates the specified string to the end of this string. |
public boolean endsWith(String suffix) | Tests if this string ends with the specified suffix. |
public boolean equals(Object anObject) | Compares this string to the specified object. |
public boolean equalsIgnoreCase(String anotherString) | Compares this String to another String, ignoring case considerations. |
public byte[] getBytes() | Convert this String into bytes according to the platform’s default character encoding, storing the result into a new byte array. |
public byte[] getBytes(String enc)
throws UnsupportedEncodingException | Convert this String into bytes according to the specified character encoding, storing the result into a new byte array. |
public byte[] getBytes(Charset charset)
throws UnsupportedEncodingException | Convert this String into bytes according to the specified character encoding, storing the result into a new byte array. |
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | Copies characters from this string into the destination character array. |
public int hashCode() | Returns a hashcode for this string. |
public int indexOf(int ch) | Returns the index within this string of the first occurrence of the specified character. |
public int indexOf(int ch, int fromIndex) | Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. |
public int indexOf(String str) | Returns the index within this string of the first occurrence of the specified substring. |
public int indexOf(String str, int fromIndex) | Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. |
public String intern() | Returns a canonical representation for the string object. |
public int lastIndexOf(int ch) | Returns the index within this string of the last occurrence of the specified character. |
public int lastIndexOf(int ch, int fromIndex) | Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. |
public int lastIndexOf(String string) | Searches in this string for the last index of the specified string. |
public int lastIndexOf(String subString, int start) | Searches in this string for the index of the specified string. |
public int length() | Returns the length of this string. |
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) | Tests if two string regions are equal. |
public String replace(char oldChar, char newChar) | Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. |
public String replace(CharSequence target, CharSequence replacement) | Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. |
public boolean startsWith(String prefix) | Tests if this string starts with the specified prefix. |
public boolean startsWith(String prefix, int toffset) | Tests if this string starts with the specified prefix beginning at the specified index. |
public String substring(int beginIndex) | Returns a new string that is a substring of this string. |
public String substring(int beginIndex, int endIndex) | Returns a new string that is a substring of this string. |
public char[] toCharArray() | Converts this string to a new character array. |
public String toLowerCase() | Converts all of the characters in this String to lower case. |
public String toString() | This object (which is already a string!) is itself returned. |
public String toUpperCase() | Converts all of the characters in this String to upper case. |
public String toUpperCase(Locale locale) | |
public String trim() | Removes white space from both ends of this string. |
public static String valueOf(boolean b) | Returns the string representation of the boolean argument. |
public static String valueOf(char c) | Returns the string representation of the char argument. |
public static String valueOf(char[] data, int offset, int count) | Returns the string representation of a specific subarray of the char array argument. |
public static String valueOf(double d) | Returns the string representation of the double argument. |
public static String valueOf(float f) | Returns the string representation of the float argument. |
public static String valueOf(int i) | Returns the string representation of the int argument. |
public static String valueOf(long l) | Returns the string representation of the long argument. |
public static String valueOf(Object obj) | Returns the string representation of the Object argument. |
public CharSequence subSequence(int start, int end) | Returns a CharSequence from the start index (inclusive) to the end index (exclusive) of this sequence. |
public static String format(String format, Object... args) | |
public boolean contains(CharSequence seq) | Checks if string contains the given char sequence. |
public boolean isEmpty() | Checks if string is empty. |
public boolean regionMatches(int thisStart, String string, int start, int length) | Compares the specified string to this string and compares the specified range of characters to determine if they are the same. |
Inherited methods
Field details
CASE_INSENSITIVE_ORDER
public static final Comparator<String> CASE_INSENSITIVE_ORDERConstructor details
String
public String()String
public String(byte[] bytes)String
public String(byte[] bytes, int off, int len)String
public String(byte[] bytes, int off, int len, String enc)
throws UnsupportedEncodingExceptionConstruct a new String by converting the specified subarray of bytes using the specified character encoding. The length of the new String is a function of the encoding, and hence may not be equal to the length of the subarray. bytes - The bytes to be converted into charactersoff - Index of the first byte to convertlen - Number of bytes to convertenc - The name of a character encoding
- If the named encoding is not supported JDK1.1
String
public String(byte[] bytes, Charset charset)
throws UnsupportedEncodingExceptionConstruct a new String by converting the specified array of bytes using the specified character encoding. The length of the new String is a function of the encoding, and hence may not be equal to the length of the byte array. bytes - The bytes to be converted into charactersenc - The name of a supported character encoding
- If the named encoding is not supported
String
public String(byte[] bytes, String enc)
throws UnsupportedEncodingExceptionConstruct a new String by converting the specified array of bytes using the specified character encoding. The length of the new String is a function of the encoding, and hence may not be equal to the length of the byte array. bytes - The bytes to be converted into charactersenc - The name of a supported character encoding
- If the named encoding is not supported JDK1.1
String
public String(char[] value)Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string. value - the initial value of the string.
- if value is null.
String
public String(char[] value, int offset, int count)Allocates a new String that contains characters from a subarray of the character array argument. The offset argument is the index of the first character of the subarray and the count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string. value - array that is the source of characters.offset - the initial offset.count - the length.
- if the offset and count arguments index characters outside the bounds of the value array.
- if value is null.
String
public String(String value)String
public String(StringBuffer buffer)Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. The contents of the string buffer are copied; subsequent modification of the string buffer does not affect the newly created string. buffer - a StringBuffer.
- If buffer is null.
String
public String(StringBuilder buffer)Method details
charAt
public char charAt(int index)Parameters
indexint- the index of the character to return.
Returns
Throws
IndexOutOfBoundsException- if
index < 0orindexis greater than the length of this sequence.
compareTo
public int compareTo(String anotherString)Parameters
anotherStringString- the object to compare to this instance.
Returns
another;
a positive integer if this instance is greater than
another; 0 if this instance has the same order as
another.Throws
ClassCastException- if
anothercannot be converted into something comparable tothisinstance.
compareToIgnoreCase
public int compareToIgnoreCase(String anotherString)contentEquals
public boolean contentEquals(CharSequence cs)contentEquals
public boolean contentEquals(StringBuffer sb)copyValueOf
public static String copyValueOf(char[] data)copyValueOf
public static String copyValueOf(char[] data, int offset, int count)concat
public String concat(String str)endsWith
public boolean endsWith(String suffix)equals
public boolean equals(Object anObject)equalsIgnoreCase
public boolean equalsIgnoreCase(String anotherString)getBytes
public byte[] getBytes()getBytes
public byte[] getBytes(String enc)
throws UnsupportedEncodingExceptiongetBytes
public byte[] getBytes(Charset charset)
throws UnsupportedEncodingExceptiongetChars
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)hashCode
public int hashCode()i is the
th character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)indexOf
public int indexOf(int ch)indexOf
public int indexOf(int ch, int fromIndex)Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. If a character with value ch occurs in the character sequence represented by this String object at an index no smaller than fromIndex, then the index of the first such occurrence is returned–that is, the smallest value k such that: (this.charAt( ) == ch) && (
= fromIndex) is true. If no such character occurs in this string at or after position fromIndex, then -1 is returned. There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned.
indexOf
public int indexOf(String str)indexOf
public int indexOf(String str, int fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the smallest value such that: this.startsWith(str, ) && (
= fromIndex) is true. There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned.
intern
public String intern()lastIndexOf
public int lastIndexOf(int ch)lastIndexOf
public int lastIndexOf(int ch, int fromIndex)lastIndexOf
public int lastIndexOf(String string)Parameters
stringString- the string to find.
Returns
Throws
NullPointerException- if
stringisnull.
lastIndexOf
public int lastIndexOf(String subString, int start)Parameters
subStringString- the string to find.
startint- the starting offset.
Returns
Throws
NullPointerException- if
subStringisnull.
length
public int length()Returns
regionMatches
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)replace
public String replace(char oldChar, char newChar)replace
public String replace(CharSequence target, CharSequence replacement)startsWith
public boolean startsWith(String prefix)startsWith
public boolean startsWith(String prefix, int toffset)substring
public String substring(int beginIndex)substring
public String substring(int beginIndex, int endIndex)toCharArray
public char[] toCharArray()toLowerCase
public String toLowerCase()toString
public String toString()toUpperCase
public String toUpperCase()toUpperCase
public String toUpperCase(Locale locale)trim
public String trim()valueOf
public static String valueOf(boolean b)valueOf
public static String valueOf(char c)valueOf
public static String valueOf(char[] data, int offset, int count)valueOf
public static String valueOf(double d)valueOf
public static String valueOf(float f)valueOf
public static String valueOf(int i)valueOf
public static String valueOf(long l)valueOf
public static String valueOf(Object obj)subSequence
public CharSequence subSequence(int start, int end)CharSequence from the start index (inclusive)
to the end index (exclusive) of this sequence.Parameters
startint- the start offset of the sub-sequence. It is inclusive, that is, the index of the first character that is included in the sub-sequence.
endint- the end offset of the sub-sequence. It is exclusive, that is, the index of the first character after those that are included in the sub-sequence
Returns
Throws
IndexOutOfBoundsException- if
start end, or ifstartorendare greater than the length of this sequence.
format
public static String format(String format, Object... args)contains
public boolean contains(CharSequence seq)Parameters
seqCharSequence- The charsequence to check
Returns
isEmpty
public boolean isEmpty()Returns
regionMatches
public boolean regionMatches(int thisStart, String string, int start, int length)Parameters
thisStartint- the starting offset in this string.
stringString- the string to compare.
startint- the starting offset in the specified string.
lengthint- the number of characters to compare.
Returns
true if the ranges of characters are equal, false
otherwiseThrows
NullPointerException- if
stringisnull.