Home > Java > javaTutorial > Remove leading zeroes from string using Apache Commons library in Java

Remove leading zeroes from string using Apache Commons library in Java

PHPz
Release: 2023-09-13 09:33:03
forward
913 people have browsed it

使用Java中的Apache Commons库从字符串中删除前导零

org.apache.commons.lang.StringUtils The stripStart() method accepts two strings and extracts them from the strings Removes the character set represented by the second string. The string of the first string.

Remove leading zeroes from string using apache common library -

  • Add the following dependencies to your pom .xml file

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.9</version>
</dependency>
Copy after login
  • Get the string.

  • Pass the obtained string as the first parameter and the string containing 0 as the second parameter to Strong>stripStart( of the < < StringUtils class ) method.

Example

The following Java program reads and converts the user's integer value into a string and uses the stripStart() method of the StringUtils class from Remove leading zeros.

import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
public class LeadingZeroesCommons {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a String: ");
      String str = sc.nextLine();
      String result = StringUtils.stripStart(str, "0");
      System.out.println(result);
   }
}
Copy after login

Output

Enter a String:
000Hello how are you
Hello how are you
Copy after login

The above is the detailed content of Remove leading zeroes from string using Apache Commons library in Java. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template