Java String strip() method Example

String strip() method


String strip() method added in jdk 11.

String strip() 

Returns a string whose value is this string, with all leading and trailing white space removed.
If this String object represents an empty string, or if all code points in this string are white space, then an empty string is returned.

Otherwise, returns a substring of this string beginning with the first code point that is not a white space up to and including the last code point that is not a white space.

This method may be used to strip white space from the beginning and end of a string.

strip method remove the extra space from both side.

Java Program Example:

class StripMethodExample{
public static void main(String args[]){
//with var variable
var test = "     hello!   ";
System.out.println(test.strip());

//with String class
String test1 = "     hello!   ";
System.out.println(test1.strip());
}
}

Output:

hello!
hello!


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation