[英]Split String with different delimiters at once
I'm getting one string from the server and want to split it, to set different colors using SpannableStringBuilder
. 我从服务器获取一个字符串,并希望将其拆分,以使用SpannableStringBuilder
设置不同的颜色。
For example, I'm getting strings like: 例如,我得到如下字符串:
" Hi Mr Xyz, Please visit [Paris::FRANCE] and [Rome::ITALY] once."
and I want to set different color to Paris and Rome . 我想给巴黎和罗马设置不同的颜色。 I'm able to split get Paris and Rome separate using : 我可以使用以下方法将巴黎和罗马分开:
Pattern pattern = Pattern.compile("\\[([^\\]]+)]");
Matcher matcher = pattern.matcher(wholeString);
while (matcher.find())
{
System.out.println(matcher.group(1));
}
But how to split main string?? 但是如何分割主字符串呢?
try like this: 尝试这样:
String wholeString = " Hi Mr Xyz, Please visit [Paris::FRANCE] and [Rome::ITALY] once.";
Pattern pattern = Pattern.compile("\\[([^\\]]+)]");
Matcher matcher = pattern.matcher(wholeString);
while (matcher.find())
{
for (int i = 0; i < matcher.group(1).split("::").length; i++) {
System.out.println(matcher.group(1).split("::")[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.