JAVA去掉字符串前面的0
2024-08-25 11:46:30
最佳方案:使用正则
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 = "";// 测试用字符串
int len = str.length();// 取得字符串的长度
int index = ;// 预定义第一个非零字符串的位置 char strs[] = str.toCharArray();// 将字符串转化成字符数组
for (int i = ; i < len; i++) {
if ('' != strs[i]) {
index = i;// 找到非零字符串并跳出
break;
}
}
String strLast = str.substring(index, len);// 截取字符串
System.out.println(strLast);// 得到结果 strLast
} }
最新文章
- radio 切换内容
- Android开发之Notification通知
- 数论初步(费马小定理) - Happy 2004
- Python - 多版本共存与虚拟独立环境
- 关于Git的stash命令
- xxx is not in the sudoers file.This incident will be reported.的解决方法
- CentOS6.3挂载读写NTFS分区
- Android:ListViewAdapter
- ice调通过iceReplica用所有server instance的方法---客户端控制服务端的负载均衡
- git编译安装与常见问题解决
- nodeJS之路径PATH模块
- 适合初学者的一个分布式环境搭建过程(spring boot + zookeeper + dubbo + mybatis + mysql)
- SBT实操指南
- 2017-9-15-Linux移植:WinSCP软件 &; SSH Server开启
- [luogu1351][联合权值]
- ORA-00600: internal error code, arguments: [kgl-no-mutex-held]
- PAT 团体程序设计天梯赛 L1-046 整除光棍(模拟除法)
- 在Objc项目中调用Swift
- 【[SHOI2007]善意的投票】
- Centos 从零开始 (二)