Apart from those methods discussed earlier, there are many other additional String methods. These methods are listed below.

Method

Description

int codePointAt(int i)

Returns the Unicode code point at the index i.

int codePointBefore(int i)

Returns the Unicode code point at the index which precedes i.

int codePointCount(int start, int end)

Returns the number of code points in the portion of invoking String between start and end-1.

int offsetByCodePoints(int start, int num)

Returns the index within the invoking string that is num codepoints beyond the starting index start.

boolean contains(CharSequence str)

Returns true if the invoking object contains the String specified by str, else it returnsfalse.

boolean contentEquals(CharSequence str)

Returns true if the invoking string contains the same String as str, else it returnsfalse.

boolean contentEquals(StringBuffer str)

Returns true if the invoking string contains the same String as str, else it returnsfalse.

static String format(String fmtstr, Object... args)

Returns a String formatted as specified by fmtstr.

static String format(Locale loc, String fmtstr, Object... args)

Returns a String formatted as specified by fmtstr. Formatting is specified by Locale loc.

boolean isEmpty()

Returns true if the invoking String contains no characters and is of length zero.

String replaceFirst(String regExp, String newStr)

Returns a Stringin which first substring that matches with regExp is replaced by newStr.

String replaceAll(String regExp, String newStr)

Returns a Stringin which all the substrings that matches with regExp are replaced by newStr.

String[] split(String regExp)

Returns an String array that decomposes the invoking string into parts on encountering regular expression specified by regExp.

String[] split(String regExp, int max)

Returns an String array that decomposes the invoking string into parts on encountering regular expression specified by regExp. The number of pieces are specified by max. If the max is negative or zero, then the invoking string is fully decomposed. If the max is positive, then the last returned array contains the remaining of the invoking string

CharSequence subSequence(int start, int stop)

Returns a substring of the invoking string that begins at start and ends at stop.

Hello--How--are--you?--Hello--How are you?--Unicode code point of element at index 0 : 97Unicode code point of element before index 1 : 97Number of code Points between indices 1 and 4 3Checks if apple contains ppl : trueContentEquals checks if the content in invoking and argument string are equal : trueReturns true when both the string patterns match : trueChecks if the string is empty :falseReplaces the first charsequence with argument passed : appletappleReplaces the charsequence with argument passed in the string : appletapplet

DESCRIPTION

The first split() divides the stringinputinto 4 parts which are stored in allTokens. The first output line displays the elements of allTokens. The second split() divides the string input into maximum of 2 parts and are stored in twoTokens. The second output line displays the elements of twoTokens.
The third output line is the display of unicode of 'a' in "apple" which is 97. The fourth output is the display of unicode of'a' which is before 'p' in "apple" which is 97. The fifth output is the display of number of unicode points between indices 1 and 4.
The sixth output is true is "apple" contains "ppl". The seventh output is true since "apple" content is equal to "apple". The eight output is true since "apple" matches with "apple". The ninth output is true since the invoked string is empty.
The tenth output is appletapple since the first substring that matches "le" is replaced with "let" in "appleapple". The eleventh output is appletapplet since all the substrings that match "le" are replaced with "let" in"appleapple".
s

THINGS TO TRY

Try this code

System.out.println("apple".codePointBefore(0));

This code throws IndexOutOfBoundsException since we are pointing to index before zero.