如何在Java中将字符串拆分为字符串和整数? [英] How to split the string into string and integer in java?

查看:62
本文介绍了如何在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])将数字字符串解析为Integer.

You might parse the digit String to Integer with Integer.parseInt(part[1]).

这篇关于如何在Java中将字符串拆分为字符串和整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆