【发布时间】:2018-03-27 01:48:32
【问题描述】:
我有一个像下面这样的字符串
String s = "John21071989Bangalore";
Class Person {
String name;
Long dob;
String city;
....
}
我有一个 bean,拆分字符串并将值设置为 Bean。
Person p = new Person();
p.setName(s.subString(0,4)); //John
p.setDob(Long.ParseLong(s.subString(4,12))); //2071989
p.setCity(s.subString(12,21));//Bangalore
现在我的问题是如果子字符串 endIndesx 小于长度给出错误。我想将 dob 值作为 null insted 错误。
String s1 = "John2107 Bangalore";
Person p = new Person();
p.setName(s.subString(0,4)); //John
p.setDob(Long.ParseLong(s.subString(4,12))); //this should be null
p.setCity(s.subString(12,21));//Bangalore
【问题讨论】:
-
长度与任何事物有什么关系?错误是因为空格