根据定界符将字符串分成2个字符串 [英] Split string into 2 strings based on a delimiter

查看:44
本文介绍了根据定界符将字符串分成2个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道是否存在基于固态库的方法来实现以下目的.

Does anybody know if there is a solid library based method to achieve the following.

说我有字符串

"name1, name2, name3, name4" 

,我想根据逗号分隔符将其解析为2个字符串.

and I want to parse it into 2 strings based on the comma delimiter.

我希望字符串看起来像这样:

I would like the strings to look like this:

string1 = "name1";
string2 = "name2, name3, name4";  

有什么想法吗?

推荐答案

Pattern pattern = Pattern.compile(", *");
Matcher matcher = pattern.matcher(inputString);
if (matcher.find()) {
    string1 = inputString.substring(0, matcher.start());
    string2 = inputString.substring(matcher.end());
}

这篇关于根据定界符将字符串分成2个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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