开发者

How to split this string?

开发者 https://www.devze.com 2023-03-13 20:20 出处:网络
How to split this string ? String s=\"{hello.World::123}\"; to Stirng array:开发者_StackOverflow {\"hello\",\"World\",\"123\"}

How to split this string ?

String s="{hello.World::123}";

to Stirng array:开发者_StackOverflow

{"hello","World","123"}

using regex ?


Try this:

 String s ="{hello.World::123}";
 System.out.println(Arrays.toString(s.split("[\\.:{}]+")));

EDIT:

Generalized solution:

 String s ="{hello.World::123}".replaceFirst("^[\\.:{}]+", " ").trim();
 System.out.println(Arrays.toString(s.split("[\\.:{}]+")));

From the comments (@Peter Lawrey):

For the particular input,

 String s ="{hello.World::123}";
 System.out.println(Arrays.toString(s.substring(1).split("[\\.:{}]+")));


Slightly modifying John's solution to avoid empty string:

String s="{hello.World::123}";
s=s.substring(1, s.length()-1);
System.out.println(Arrays.toString(s.split("[\\.:]+")));


Sorry for the inconvenience in my previous answer.

Here is something that may actually work:

String[] a = s.replaceAll("[\\{\\}]", "").split("\\.|::");

However, this will not allow you to have { or } as elements in the list, but I guess you don't have that.


I don't think it's a simple, single split. Your struggles might be telling you the same thing.

You can use regex to match on more than one word character and return each group, but I don't believe one call to split will do it.


How about :

s.split("\\.|::");
0

精彩评论

暂无评论...
验证码 换一张
取 消