java 中,如果输入一串数字,如“000200”,我想把前边的“000”去掉,变成“200”,该怎么办?

[复制链接]
查看11 | 回复4 | 2008-3-21 15:08:17 | 显示全部楼层 |阅读模式
public class Test { public static void main(String[] args) {String s="000200";System.out.println(Integer.parseInt(s)); }}
回复

使用道具 举报

千问 | 2008-3-21 15:08:17 | 显示全部楼层
假设输入到字符串str中,str=Long.toString(Long.parseLong(str));使用long不用int的理由是这个字符串可能很长如果字符串不能转换成数字,会抛出NumberFormatException异常。因此较好的编程习惯是捕捉这个异常。
回复

使用道具 举报

千问 | 2008-3-21 15:08:17 | 显示全部楼层
String str = "000200";System.out.println(str.subString(3));怎么样,我的这种是不是更为简单啊?
回复

使用道具 举报

千问 | 2008-3-21 15:08:17 | 显示全部楼层
最简单的方法就是拿int类型的变量接受这个数字,呵呵!
回复

使用道具 举报

千问 | 2008-3-21 15:08:17 | 显示全部楼层
你给的题目的答案是这样的:String str = "000200";System.out.println(str.substring(3, 6));我已经运行过了,没问题的。其实JDK文档是很有用的东西,有什么问题可以参考一下,关于你的问题,JDK文档是这样写的:public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Examples:
"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)
public String substring(int beginIndex,int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
Examples:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行