Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, February 11, 2019

Java 11 String strip(), stripLeading(), stripTrailing() Examples

String 11 strip() method:

Strip has three methods.

1) strip()
2) stripLeading()
3) stripTrailing

In this post, we will learn how to use strip(), stripLeading(), stripTrailing() methods that are introduced in Java 11 version.

public String strip():


strip() method removes all leading and trailing white space and returns a new string.

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.

public class StringStripExample {
    public static void main(String[] args) {

        String input = "   hello   ";
        System.out.println("Input string length : " + input.length());
        String stripStr = input.strip();
        System.out.println("Stripped string length : " + stripStr.length());
    }
}

Output:

Input string length : 11
Stripped string length : 5

strip() method removes total 6 white spaces from input.

strip() method internal code:

  public String strip() {
        String ret = isLatin1() ? StringLatin1.strip(value)
                                : StringUTF16.strip(value);
        return ret == null ? this : ret;
    }
   

public String stripLeading():


Returns a new string with all leading white spaces is removed. leading white spaces means if any white spaces before first unicode character.

This is mostly useful if you want to remove white spaces only at the beginning of a string.

      
        String input = "   hello   ";
        System.out.println("Input string length : " + input.length());
        String stripStr = input.stripLeading();
        System.out.println("Stripped string length : " + stripStr.length());
        System.out.println("Input String :"+input);
        System.out.println("Output String :"+stripStr);
       

Output:


Input string length : 11
Stripped string length : 8
Input String :   hello  
Output String :hello 

From the output, only three white spaces from the beginning of the string.

public String stripTrailing():


Used to remove white spaces only at the ending of a string.
        String input = "   hello   ";
        System.out.println("Input string length : " + input.length());
        String stripStr = input.stripTrailing();
        System.out.println("Stripped string length : " + stripStr.length());
        System.out.println("Input String :"+input);
        System.out.println("Output String :"+stripStr);

Output:


Input string length : 11
Stripped string length : 8
Input String :   hello  
Output String :   hello

Please post your questions in comment section.

No comments:

Post a Comment

Please do not add any spam links in the comments section.