'\n': line feed
'\r': carriage return
'f': form feed
'\t': horizontal tab
In a large text, certain phrases that are required have to lead and/or trailing whitespaces. When such a phrase is extracted, it may come with leading and/or trailing whitespace. No matter how a useful phrase is obtained, the leading or trailing whitespace has to be removed for the phrase to be properly used.
In the java.lang package, there is the string class. This string class has a method called trim(). The trim() method removes leading and/or trailing space from a string. It returns the string with the leading and/or trailing whitespace removed.
In Java, the string class does not have to be imported; it is imported automatically. In order to use the string class in code, the string class has to be typed as “String”, with the first letter in uppercase.
Demonstrating the Effect of Whitespace
Leading and/or trailing whitespace can be a nuisance. Compile and run the following program:
In the author’s computer, the “useful part” was printed with blank lines above and below it.
Using the trim() Method
The trim() method is simple to use. The syntax is:
The following program illustrates its use:
The output is:
without any leading or trailing whitespace.
Handling Input from Console
Java has several ways of getting input from the keyboard into the program. One of the ways uses the Scanner class. The Scanner class is in the java.util package. The following program shows how to get a line from the keyboard into the program:
The first line of the program imports the Scanner class. After that, there is the main class definition (implementation). The main class has the main method. In the main() method, the first statement instantiates the scanner object. The next statement prints text to the console, asking the user to type in a phrase. At this point, the program waits for the user to type in a phrase.
The next statement reads in the input line into the variable, phrase. The following statement in the main() method re-displays this phrase as it was typed, with any leading or trailing space. The last statement in the main() method closes the scanner object.
Leading or trailing spaces are usually not wanted from the keyboard input. It is simple to remove them; by just using the trim() method of the string object. The following program illustrates this:
Leading or trailing spaces typed with the space-bar key were removed.
Trimming with Regular Expression
With Java regular expression, the trim() method does not have to be used. A regular expression is an expression whose main component is a pattern. A pattern is a string with string meta-characters. A pattern will identify a sub-string with particular characters in a target string. Such a sub-string identified in the target string can be replaced. A regular expression is a construct that holds the pattern.
The target string may be the input string read from the keyboard. The sub-string to be identified in this topic is the leading and/or trailing whitespace. Remember, this whitespace consists of one or more of the different whitespace characters mentioned above. When this whitespace is found at the beginning or end of the target string, it is replaced with nothing.
The pattern for this whitespace is [\u0020\t\n\r\f]* or \s*. \s means [\u0020\t\n\r\f]. The regular expression to match the leading or trailing whitespace is:
The string class has the replaceAll() method that can be used to remove the leading and trailing space from the target string. In the following program, rawStr is the string with whitespaces. There are two words, “one” and “two,” in this string. There are white spaces in front of “one”, after “two,” and in-between “one” and “two”. The program successfully removes the leading and trailing white spaces and not the white spaces between “one” and “two”. refinedStr is the string variable without the leading and trailing white spaces. The first argument to replaceAll() is the regular expression. The second argument is the replacement, which in this case, is an empty string (without even the spacebar character). The program is:
So at the output, there is a blank line between “one” and “two”.
Trimming a string means removing the leading and trailing whitespaces. A space here consists of one or more of ‘ ‘ or \t or \n or \r or \f . Any combination of these characters within the text is not removed. The best way to remove leading and to trail space from a string is to use the trim() method of the string class. The string class also has the replaceAll() method, which can be used for trimming. However, the replaceAll() method needs knowledge and experience in using regular expression techniques, as illustrated above. The first argument to replaceAll() is the regular expression; the second argument is the replacement text, which in this case, has to be “.