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
        }
    
    }
  • 相关阅读:
    jquery02
    jquery01
    oracle04_plsql
    oracle03
    oracle02
    oracle01
    selenium
    爬取京东商品信息并保存到MongoDB
    python pymongo操作之增删改查
    MongoDB 数据库表删除
  • 原文地址:https://www.cnblogs.com/exmyth/p/4897768.html
Copyright © 2011-2022 走看看