Menu
Topics Index
...
`

Additional String Methods - Java Example Program


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?--

© meritcampus 2019

All Rights Reserved.

Open In App