87
The following is an example.
System.out.println("".isEmpty());
prints True in the Console pane, while:
System.out.println(" ".isEmpty());
prints False in the Console pane.
In Java 11 the new isBlank() method is equal to trim().isEmpty().
3-6. Stripping Whitespace
Problem
You want to remove whitespace.
Solution
Use the java.lang.String.strip() method, which deletes whitespaces,
as shown in
Listing
3-1
.
Listing 3-1. Using the strip Method
class StripClassExample
{
public static void main(String[] args)
{
String nameString = " Ciao Sara! ";
System.out.println( nameString.strip() );
System.out.println( nameString.stripLeading() );
System.out.println( nameString.stripTrailing() );
}
}
It returns the following output.
//"Ciao Sara!"
//"Ciao Sara! "
//" Ciao Sara!"
Chapter 3 StringS
88
How It Works
The strip method was introduced in Java 11 to remove
the whitespace from both,
beginning and the end of the string. It produces the same result of trim(), while
stripLeading() removes the whitespace from the beginning and stripTrailing()
removes
the whitespace from the end.
Do'stlaringiz bilan baham: