如何在java中将字符串拆分为字符串和整数 [英] how to split the string into string and integer in java
本文介绍了如何在java中将字符串拆分为字符串和整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有字符串a =abcd1234
我希望将其拆分为字符串b =abcd
和 Int c = 1234
此分割代码应适用于所有输入王,如ab123456和acff432等。
如何分割这种字符串。有可能吗?
I have the String a="abcd1234"
and i want to split this into String b="abcd"
and Int c=1234
This Split code should apply for all king of input like ab123456 and acff432 and so on.
How to split this kind of Strings. Is it possible?
推荐答案
您可以尝试拆分正常表达式,例如(?< = \D)(?= \d)
。试试这个:
You could try to split on a regular expression like (?<=\D)(?=\d)
. Try this one:
String str = "abcd1234";
String[] part = str.split("(?<=\\D)(?=\\d)");
System.out.println(part[0]);
System.out.println(part[1]);
将输出
abcd
1234
你可以用<解析数字字符串到整数code> Integer.parseInt(part [1])。
这篇关于如何在java中将字符串拆分为字符串和整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文