Java - String stripLeading() Method



The Java String stripLeading() method is used to remove the whitespaces from the beginning of the given string. It retrieves a copy of the string with leading spaces omitted or the same string is retrieved if it has no leading whitespace.

This method results in an empty string if the given string contains only white spaces. Similar to thestrip() method, the stripLeading() methodcan also remove space characters as per the Unicode standards having ASCII value greater than 32(\u0020).

Syntax

Following is the syntax for Java String stripLeading() method −

public String stripLeading()

Parameters

This method does not accept any parameter.

Return Value

This method returns a copy of this string with leading white space removed, or this string if it has no leading white space.

Example

The following example shows the usage of Java String stripLeading() method by removing the leading white spaces from the string "    This is TutorialsPoint" −

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      
      // string with leading white space
      String str = "    This is TutorialsPoint";
      System.out.print("Before removing spaces = ");
      System.out.println(".." + str + "..");
      
      // leading white space removed
      System.out.print("After removing spaces = ");
      System.out.println(".." + str.stripLeading() + "..");
   }
}

Output

If you compile and run the above program, it will produce the following result −

Before removing spaces = ..    This is TutorialsPoint..
After removing spaces = ..This is TutorialsPoint..

Example

Below is an example to remove all the leading spaces using stripLeading() method in Java. After removing the spaces we are verifying the length of the string using the length() function −

import java.util.Locale;
public class StringDemo {
   public static void main(String[] args) {
      String str = "    Tutorials Point      ";
      System.out.println("The length of the string before removing spaces is: " + str.length());
      System.out.println("The string without removing spaces is: " + str);
      String t = str.stripLeading();
      System.out.println("The length of the string after removing leading spaces is: " + t.length());
      System.out.println("The string with removed spaces is:" + t);
   }
}

Output

If you compile and run the program above, the output will be displayed as follows −

The length of the string before removing spaces is: 25
The string without removing spaces is:     Tutorials Point
The length of the string after removing leading spaces is: 21
The string with removed spaces is:Tutorials Point

Example

In the code given below the stripLeading() method is invoked on two strings 's1' and 's2' respectively. The second string has a space character with a codepoint value > 32(\u0020). Then the spaces present at the beginning of the string gets removed, whereas the ones at the end doesn't −

public class StringDemo {
   public static void main(String[] args) {
      String s1 = "   Java Programming ";
      System.out.println("The first string given is: " + ".." + s1 + "..");
      
      // string with the whitespace character as per the Unicode values
      String s2 = '\u2003' + "Java Programming ";
      System.out.println("The second string is: " + ".." + s2 + "..");
      System.out.println("The first string after removing leading spaces is : " + ".." + s1.stripLeading() + "..");
      System.out.println("The second string after removing leading spaces is: " + ".." + s2.stripLeading() + "..");
   }
}

Output

On executing the program above, the output is obtained as follows −

The first string given is: ..   Java Programming ..
The second string is: ..?Java Programming ..
The first string after removing leading spaces is : ..Java Programming ..
The second string after removing leading spaces is: ..Java Programming ..

Example

Since strings in Java are immutable, while removing the leading white spaces from a string using the stripLeading() method, a new string is returned. The reference to the same string is returned if the manipulation is not carried out by the stripLeading() method as shown in the following example −

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "    Tutorials Point";
      System.out.println("The given string is: " + ".." + s + "..");
      String s1 = s.stripLeading();
      System.out.println("The string after removing leading whitespace is: " + ".." + s1 + "..");
      
      // the hashcode of s and s1 is different
      System.out.println("The hashcode of string s is: " + s.hashCode());
      System.out.println("The hashcode of string s1 is: " + s1.hashCode());
      String x = "calm";
      String y = x.stripLeading();
      
      // the hashcode of x and y is the same
      System.out.println("The hashcode of string x is: " + x.hashCode());
      System.out.println("The hashcode of string y is: " + y.hashCode());
   }
}

Output

The output of the above program is as follows −

The given string is: ..    Tutorials Point..
The string after removing leading whitespace is: ..Tutorials Point..
The hashcode of string s is: 776517189
The hashcode of string s1 is: -1622622651
The hashcode of string x is: 3045983
The hashcode of string y is: 3045983
java_lang_string.htm
Advertisements