zoukankan      html  css  js  c++  java
  • 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
        }
    
    }
  • 相关阅读:
    HDU5914
    HDU1087(dp)
    HDU1711(KMP)
    HDU1251(字典树)
    HDU3068(Manacher算法)
    POJ2187(旋转卡壳)
    HDU1392(凸包)
    CodeForces 722B
    CodeForces 722A
    CodeForces 721B
  • 原文地址:https://www.cnblogs.com/exmyth/p/4897768.html
Copyright © 2011-2022 走看看