
org.apache.commons.lang.StringUtils 类的 stripStart() 方法接受两个字符串,并从字符串中删除第二个字符串表示的字符集。第一个字符串的字符串。
使用 apache 公共库从字符串中删除前导零 -
将以下依赖项添加到您的 pom .xml 文件
org.apache.commons commons-lang3 3.9
-
获取字符串。
立即学习“Java免费学习笔记(深入)”;
将获取的字符串作为第一个参数,将包含 0 的字符串作为第二个参数传递给 StringUtils 类的 Strong>stripStart() 方法。
示例
以下 Java 程序读取将用户的整数值转换为字符串,并使用 StringUtils 类的 stripStart() 方法从中删除前导零。
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);
}
}输出
Enter a String: 000Hello how are you Hello how are you











