简体   繁体   中英

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 .

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]);
                }
             }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM