java中如何将字符串拆分为字符串和整数? [英] How to split the string into string and integer in java?
本文介绍了java中如何将字符串拆分为字符串和整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 String a=abcd1234"
并且我想将其拆分为 String 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
您可以使用 Integer.parseInt(part[1])
将数字字符串解析为整数.
You might parse the digit String to Integer with Integer.parseInt(part[1])
.
这篇关于java中如何将字符串拆分为字符串和整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文