Extracting Substrings from Strings – Selected API Classes

Extracting Substrings from Strings

The String class provides methods to trim and strip strings, and also extract substrings.

boolean isBlank()

Returns true if the string is empty or contains only whitespace; otherwise, it returns false. See also the method isEmpty() in the CharSequence interface (p. 444).

String strip()
String stripLeading()
String stripTrailing()

Return a string whose value is this string, with all leading and trailing whitespace removed, or with all leading whitespace removed, or with all trailing whitespace removed, respectively. If this String object represents an empty string, or if all characters in this string are whitespace, then an empty string is returned. See also the method stripIndent() (p. 444).

String trim()

This method can be used to create a string where all characters with values less than or equal to the space character ‘\u0020’ have been removed from the front (leading) and the end (trailing) of a string. It is recommended to use the strip methods to remove leading and trailing whitespace in a string.

Click here to view code image

String substring(int startIndex)
String substring(int startIndex, int endIndex)

The String class provides these overloaded methods to extract substrings from a string. A new String object containing the substring is created and returned. The first method extracts the string that starts at the given index startIndex and extends to the end of the string. The end of the substring can be specified by using a second argument endIndex that is the index of the first character after the substring—that is, the last character in the substring is at index endIndex-1. If the index value is not valid, an IndexOutOfBoundsException is thrown.

Click here to view code image

CharSequence subSequence(int start, int end)
From the
 CharSequence
interface
(
p. 444
)

This method behaves the same way as the substring(int start, int end) method.

The Character.isWhitespace() method can be used to determine whether a character is whitespace.

Click here to view code image

System.out.println(Character.isWhitespace(‘\t’));  // true
System.out.println(Character.isWhitespace(‘\n’));  // true
System.out.println(Character.isWhitespace(‘a’));   // false

Examples of blank strings:

Click here to view code image

System.out.println(“”.isBlank());        // true
System.out.println(” \t  “.isBlank());   // true

Examples of stripping a string:

Click here to view code image

String utopia = “\t\n  Java Nation \n\t  “;
System.out.println(utopia.strip().equals(“Java Nation”));               // true
System.out.println(utopia.stripLeading().equals(“Java Nation \n\t  “)); // true
System.out.println(utopia.stripTrailing().equals(“\t\n  Java Nation”)); // true

Examples of extracting substrings:

Click here to view code image

String utopia2 = “\t\n  Java Nation \n\t  “;
utopia2 = utopia2.trim();                       // “Java Nation”
utopia2 = utopia2.substring(5);                 // “Nation”
String radioactive = utopia2.substring(3,6);    // “ion”


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *