In Java, removing whitespace from a given String is a common need when manipulating strings. This article will discuss eliminating all or a portion of the existing whitespace in the string. We'll look at numerous built-in Java methods to accomplish the task of remove whitespaces from a string: The article aims to: Suppose you have to display a list of strings, but each of these strings has some spaces at the end, added mistakenly. This needs to be removed before displaying. Likewise, there can be spaces in between or at the
beginning of the strings as well. We shall be looking at Java programs to remove these whitespaces. Also, Java provides numerous in-built methods to perform this task. To list a few: Let us discuss each of these methods to remove unnecessary spaces from a String one by one. trim() method in JavaThis is the most common method to remove white spaces present at the beginning and end of the string. For trim() method, space character is a character having ASCII values <= 32. The Unicode value of space character is '\u0020'. It removes all characters between '\u0000' (NUL) and '\u0020' (SPACE) from the start and end of the string. It checks for these Unicode values at the beginning and end of the string. If present, it removes the spaces and returns the modified string. Signaturetrim() method is defined as: Syntaxtrim() method is written as Parameterstrim() method takes no parameters. Return ValuesIt returns the modified version of the original string, with no spaces at the end and beginning. ExceptionsIt throws
Usage of trim() methodProgram to Remove Leading and Trailing Whitespaces
Output:
Explanation: The trim() method is used in the program above to trim the strings s and s1. In contrast to s1, which contains numerous spaces, s only has one space at the beginning and one at the end. These spaces are eliminated once the method trim() is called and the output is printed. Program where exception is thrown: Let us look at an example where we call trim() method on a null string. Code:
Output:
Explanation: In the above program, the value of the string is null; hence exception is thrown. strip() method Java 11The strip() method was introduced in Java 11. It also removes leading and trailing spaces present in the string. But that is exactly what trim() does. What's the difference between the two? strip() method is Unicode aware, a modification over trim().The reason behind the addition of this method is that there exist numerous space characters as per the Unicode standards having ASCII value > 32(‘U+0020’); the strip() method can remove them as well.
Signaturestrip() method is defined as: Syntaxstrip() method is written as Parametersstrip() method takes no parameters. Return ValuesIt returns the modified version of the original string, i.e., with no spaces at the end and beginning. ExceptionsIt throws
Usage of strip() methodProgram to Remove Leading and Trailing Whitespaces
Output:
Explanation: In the above program, we have appended a whitespace character with Unicode value: ‘\u2003’. So as you can see, the trim() method does not remove it as the value is not less than '\u0020'. Whereas in the case of strip(), it gets removed. This pretty much explains the advantage of strip() over trim(). stripLeading() method Java 11stripLeading() method removes all the leading spaces or the spaces present at the beginning of the string. It was introduced in Java 11. Just like strip(), it can remove the space characters as per the Unicode standards having ASCII value greater than 32(‘\u0020’) as well. SignaturestripLeading() method is defined as:
SyntaxstripLeading() method is written as ParametersstripLeading() method takes no parameters. Return ValuesIt returns the modified version of the original string, i.e., with no spaces at the beginning. Exceptions: It throws
Usage of stripLeading() methodProgram to Remove Leading Whitespaces
Output:
Explanation: In the above program, the stripLeading() method is called on two strings. One of these has a space character with a codepoint value > 32(‘\u0020’). As you can see, in both cases, the spaces present at the beginning get removed; however, the ones at the end are still present. stripTrailing() method Java 11This method is exactly the opposite of the stripLeading() method. It removes all the trailing or the spaces present at the end of the string. It was also introduced in Java11. See the image below for reference: SignaturestripTrailing() method is defined as:
SyntaxstripTrailing() method is written as ParametersstripTrailing method takes no parameters. Return ValuesIt returns the modified string with no spaces at the end. ExceptionsIt throws
Usage of stripTrailing() methodProgram to Remove Trailing Whitespaces
Output:
Explanation: In the above program stripLeading() method is called on two strings. One of these has a space character with a codepoint value > 32(‘\u0020’). As you can see, in both cases, the spaces present at the end get removed. However, the ones at the beginning are still present. replaceAll(String regex, String replacement)replaceAll() is a very commonly used method for string manipulation in Java. This method replaces all the substrings of the string matching the specified regex expression with the specified replacement text. A regular expression or regex is a string pattern that we use for some searching or matching kind of operations. We can use this method to remove spaces from the string. All we need to do is specify the regex expression for the kind of spaces that need to be removed, i.e., trailing or leading, or both. The following table shows the regex expressions for the same:
SignaturereplaceAll() method is defined as:
SyntaxreplaceAll() method is written as
ParametersreplaceAll method takes two parameters:
Return ValuesIt returns the modified string, where all the occurrences of matched substrings are replaced by the string specified as a parameter. ExceptionsIt throws :
Usage of replaceAll() methodProgram to Remove All Whitespaces and Leading, Trailing Whitespaces
Output
Explanation In the above program, replaceAll() is called with different regular expressions as parameters to remove leading, trailing, and all the spaces. Program where Exception is thrown Code:
Output:
Explanation: So in the above program, the syntax defined for regex is incorrect; hence exception is thrown. Note: We can not directly use "" in Java, escape character is required. Thus, "\" is used instead. Conclusion
Which of these methods of class string is used to remove leading and trailing Whitespaces trim () do trim () trim ()) Startswith ()?trim() The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string.
How do you remove leading and trailing whitespaces from a string?We can eliminate the leading and trailing spaces of a string in Java with the help of trim(). trim() method is defined under the String class of java. lang package.
Which function is used to remove leading and trailing Whitespaces?The Trim function
The most obvious (and generally efficient) method for removing both leading and trailing space is to use the TRIM() function.
Which of these method of class string is used to remove Whitespaces?Explanation: trim() method is used to remove leading and trailing whitespaces in a string.
|