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
    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM