软件编程
位置:首页>> 软件编程>> java编程>> Java去掉数字字符串开头的0三种方法(推荐)

Java去掉数字字符串开头的0三种方法(推荐)

作者:jingxian  发布时间:2022-05-31 08:04:41 

标签:java,去掉,字符串,开头的0,数字

方式一:

例如:”0000123” (字符串必须全为数字)

处理过程:


String tempStr = "0000123";
int result = Integer.parseInt(tempStr);

result 结果:123

方式二:

例如:”0000123”

处理过程:


String str = "0000123";
String newStr = str.replaceFirst("^0*", "");
System.out.println(newStr);

打印结果:123

方式三:

例如:”0000123”

处理过程:


String str = "0000123";
String newStr = str.replaceAll("^(0+)", "");
System.out.println(newStr);

打印结果:123

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com