Menu
Topics Index
...
`


Strings >
Siva Nookala - 20 Feb 2017
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.
The following example program illustrates some of these methods
Additional String Methods
class AdditionalStringMethods
{
    public static void main(String arg[])
    {
        String input = "Hello How are you?";
        String[] allTokens = input.split(" ");
        String[] twoTokens = input.split(" ", 2);
        for (String element : allTokens)
        {
            System.out.print(element + "--");
        }
        System.out.println();
        for (String element : twoTokens)
        {
            System.out.print(element + "--");
        }
        System.out.println();
        //Notice String apple is used here to explain String methods
        System.out.println("Unicode code point of element at index 0 : " + "apple".codePointAt(0));
        System.out.println("Unicode code point of element before index 1 : " + "apple".codePointBefore(1));
        System.out.println("Number of code Points between indices 1 and 4 " + "apple".codePointCount(1, 4));
        System.out.println("Checks if apple contains ppl : " + "apple".contains("ppl"));
        System.out.println("ContentEquals checks if the content in invoking and argument string are equal : " + "apple".contentEquals("apple"));
        System.out.println("Returns true when both the string patterns match : " + "apple".matches("apple"));
        System.out.println("Checks if the string is empty :" + " ".isEmpty());
        System.out.println("Replaces the first charsequence with argument passed : " + "appleapple".replaceFirst("le", "let"));
        System.out.println("Replaces the charsequence with argument passed in the string : " + "appleapple".replaceAll("le", "let"));    
    }
}
OUTPUT

Hello--How--are--you?--
Hello--How are you?--
Unicode code point of element at index 0 : 97
Unicode code point of element before index 1 : 97
Number of code Points between indices 1 and 4 3
Checks if apple contains ppl : true
ContentEquals checks if the content in invoking and argument string are equal : true
Returns true when both the string patterns match : true
Checks if the string is empty :false
Replaces the first charsequence with argument passed : appletapple
Replaces 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.
  • Try this code
    System.out.println("apple".contains("plp"));
    System.out.println("apple".contentEquals("pplea"));
    The output for above code is :
    false
    false

    Since apple doesn't contain plp or pplea.
  • Try this code
    String input = "Hello How are you?";
    String[] tokens = str.split(" ", 6);
    for (int i = 0; i < tokens.length; i++)
    {
        System.out.print(tokens[i] + "--");
    }
    In the above example we can split input into maximum of 4 strings, but the maximum number of parts is specified as 6. However it will spilt into 4 parts only.
    Hello--How--are--you?--

Dependent Topics : Java String Comparison Methods - Equals and EqualsIgnoreCase  Java regionMatches() Method - String Comparison  Java equals method vs == Operator  Additional StringBuffer Methods In Java 

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App