我有一个字符串,"004-034556"我想分成两个字符串:string1="004";string2="034556";这意味着第一个字符串将包含之前的字符'-',第二个字符串将包含之后的字符'-'。我还想检查字符串是否包含'-'在其中。如果没有,我会抛出异常。我怎样才能做到这一点?如何在Java中拆分字符串
4 回答
手掌心
TA贡献1942条经验 获得超3个赞
// This leaves the regexes issue out of question
// But we must remember that each character in the Delimiter String is treated
// like a single delimiter
public static String[] SplitUsingTokenizer(String subject, String delimiters) {
StringTokenizer strTkn = new StringTokenizer(subject, delimiters);
ArrayList<String> arrLis = new ArrayList<String>(subject.length());
while(strTkn.hasMoreTokens())
arrLis.add(strTkn.nextToken());
return arrLis.toArray(new String[0]);
}
添加回答
举报
0/150
提交
取消