String class common methods

1 char charAt(int index)Returns the char value at the specified index.
2 int compareTo(Object o)Compare the string to another object.
3 int compareTo(String anotherString)Compare two strings in lexicographical order.
4 int compareToIgnoreCase(String str)Compares two strings in lexicographical order, regardless of case.
5 String concat(String str)Concatenates the specified string to the end of this string.
6 boolean contentEquals(StringBuffer sb)Return true if and only if the string has characters in the same order as the specified StringBuffer.
7 static String copyValueOf(char[] data)Returns a String representing the sequence of characters in the specified array.
8 static String copyValueOf(char[] data, int offset, int count)Returns a String representing the sequence of characters in the specified array.
9 boolean endsWith(String suffix)Tests whether the string ends with the specified suffix.
10 boolean equals(Object anObject)Compares this string to the specified object.
11 boolean equalsIgnoreCase(String anotherString)Compare this String with another String, regardless of case.
12 byte[] getBytes()Encode this String as a sequence of bytes using the platform’s default character set and store the result in a new byte array.
13 byte[] getBytes(String charsetName)Encodes this String as a sequence of bytes using the specified character set, and stores the result in a new byte array.
14 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Copies characters from this string to the target character array.
15 int hashCode()Returns the hash code for this string.
16 int indexOf(int ch)Returns the index of the first occurrence of the specified character in this string.
17 int indexOf(int ch, int fromIndex)Returns the index at the first occurrence of the specified character in this string, starting with the specified index.
18 int indexOf(String str)Returns the index of the first occurrence of the specified substring in this string.
19 int indexOf(String str, int fromIndex)Returns the index of the first occurrence of the specified substring in this string, starting at the specified index.
20 String intern()Returns the normalized representation of a string object.
21 int lastIndexOf(int ch)Returns the index at the last occurrence of the specified character in this string.
22 int lastIndexOf(int ch, int fromIndex)Returns the index of the last occurrence of the specified character in this string, starting at the specified index.
23 int lastIndexOf(String str)Returns the index of the rightmost occurrence of the specified substring in this string.
24 int lastIndexOf(String str, int fromIndex)Returns the index of the last occurrence of the specified substring in this string, searching backwards from the specified index.
25 int length()Returns the length of this string.
26 boolean matches(String regex)Tells if the string matches the given regular expression.
27 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)Tests whether two string regions are equal.
28 boolean regionMatches(int toffset, String other, int ooffset, int len)Tests whether two string regions are equal.
29 String replace(char oldChar, char newChar)Returns a new string obtained by replacing all oldChar occurrences in the string with newChar.
30 String replaceAll(String regex, String replacement)Replaces all substrings of this string that match the given regular expression with the given replacement.
31 String replaceFirst(String regex, String replacement)Replaces the first substring of this string that matches the given regular expression with the given replacement.
32 String[] split(String regex)Splits the string based on the match of the given regular expression.
33 String[] split(String regex, int limit)Splits the string based on matching the given regular expression.
34 boolean startsWith(String prefix)Tests whether the string starts with the specified prefix.
35 boolean startsWith(String prefix, int toffset)Tests whether substrings of this string starting at the specified index start with a specified prefix.
36 CharSequence subSequence(int beginIndex, int endIndex)Returns a new sequence of characters that is a subsequence of this sequence.
37 String substring(int beginIndex)Returns a new string that is a substring of the string.
38 String substring(int beginIndex, int endIndex)Returns a new string that is a substring of the string.
39 char[] toCharArray()Converts this string to a new character array.
40 String toLowerCase()The rules that use the default locale convert all characters in this String to lowercase.
41 String toLowerCase(Locale locale)Converts all characters in this String to lowercase using the rules for the given Locale.
42 String toString()Returns the object itself (which is already a string!) .
43 String toUpperCase()The rules that use the default locale convert all characters in this String to uppercase.
44 String toUpperCase(Locale locale)Converts all characters in this String to uppercase using the rules for the given Locale.
45 String trim()Returns a copy of the string, ignoring leading and trailing whitespace.
46 static String valueOf(primitive data type x)Returns the string representation of the given data type x parameter.
47 contains(CharSequence chars)Determines whether the specified character series is included.
48 isEmpty()Determines whether the string is empty.

StringBuffer

Here are the main methods supported by the StringBuffer class:

1 Public StringBuffer append(String s) Appends the specified String to this character sequence.
2 Public StringBuffer reverse() replaces this character sequence with its reversed form.
3 Public delete(int start, int end) remove characters from substrings of this sequence.
4 Public insert(int offset, int IintThe string representation of the argument is inserted into this sequence.
5 Insert (int offset, String STRstrParameter is inserted into this sequence.
6 Replace (int start, int end, String STRStringReplaces characters in substrings of this sequence with characters in

The following list lists other common methods of the StringBuffer class:

The serial number Methods described
1 Int capacity() Returns the current capacity.
2 Char charAt(int index) Returns the value at the specified index in this sequencecharValue.
3 Void ensureCapacity(int minimumCapacity) ensures that the capacity is at least equal to the specified minimum.
4 Void getChars(int srcBegin, int srcEnd, char[] DST, int dstBegin) copies characters from this sequence to target character arraydst.
5 Int indexOf(String STR) returns the indexOf the specified substring in the String for the first occurrence.
6 Int indexOf(String STR, int fromIndex) returns the indexOf the first occurrence of the specified substring in the String, starting at the specified index.
7 Int lastIndexOf(String STR) Returns the index of the specified substring that appears in the rightmost part of the String.
8 Int lastIndexOf(String STR, int fromIndex) Mandatory String Last position of the neutron in the object.
9 Int length() returns the length(in characters).
10 Void setCharAt(int index, char ch) sets the character at the given index toch.
11 Void setLength(int newLength) sets the length of the character sequence.
12 CharSequence subSequence(int start, int end) returns a new character sequence that is a subSequence of this sequence.
13 String substring(int start) Returns a new oneStringWhich contains the character subsequence that this character sequence currently contains.
14 String substring(int start, int end) returns a new oneStringWhich contains the character subsequence that this sequence currently contains.
15 String toString() returns a String representation of the data in this sequence.

Similarities and differences

String: an immutable sequence of characters; The underlying storage is char[]

StringBuffer: variable sequence of strings, thread safe, inefficient; The underlying storage is char[]

StringBuilder: variable string sequence, thread unsafe, high efficiency; The underlying storage is char[]