JAVA去掉字符串前面的0

最佳方案:使用正则

String str = "000000001234034120";
String newStr = str.replaceAll("^(0+)", "");
System.out.println(newStr);
package com.exmyth.test.string;

public class StrTest04 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "00001102";// 测试用字符串
        int len = str.length();// 取得字符串的长度
        int index = 0;// 预定义第一个非零字符串的位置

        char strs[] = str.toCharArray();// 将字符串转化成字符数组
        for (int i = 0; i < len; i++) {
            if ('0' != strs[i]) {
                index = i;// 找到非零字符串并跳出
                break;
            }
        }
        String strLast = str.substring(index, len);// 截取字符串
        System.out.println(strLast);// 得到结果 strLast
    }

}
原文地址:https://www.cnblogs.com/exmyth/p/4897768.html