欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA 中去除字符串前面的0

程序员文章站 2022-05-24 12:29:49
...

虽然很简单,但是还是记录一下

 

方法一

public class Solution{
    public static void main(String[] args){
        String result = "0000123456";
        System.out.println("This is original result:"+result);
        result = Integer.toString(Integer.parseInt(result)); //这一句
        System.out.println("This is output result: "+result);

    }
}



输出结果:
This is original result:0000123456
This is output result: 123456

方法二

public class Solution{
    public static void main(String[] args){
        String result = "00001234560";
        System.out.println("This is original result:"+result);
        while (result.length()>0 && result.charAt(0) == '0'){
            result = result.replaceFirst("0",""); //这一句
        }

        System.out.println("This is output result: "+result);

    }
}



输出结果:
This is original result:00001234560
This is output result: 1234560