Reputation: 117
i tried to split a string looks like this
<a><b><c></c></b></a>
with java using the following code
String[] s =input.split("[<>]+");
System.out.println(Arrays.toString(s));
and this was the output
[, a, b, c, /c, /b, /a]
i don't know what should i do to get rid of this empty string in the beginning of the resulting array.
any suggestions ?
Upvotes: 1
Views: 728
Reputation: 2211
There is an alternative method using regular expression matching:
String input = "<a><b><c></c></b></a>";
Pattern p = Pattern.compile("<(.+?)>");
Matcher m = p.matcher(input);
ArrayList<String> s = new ArrayList<String>();
while(m.find())
s.add(m.group(1));
System.out.println(s.toString());
Output: [a, b, c, /c, /b, /a]
Upvotes: 4
Reputation: 3701
If you are up for using Google Guava library, then Splitter has a neat way to omit empty strings using Splitter#omitEmptyStrings()
.
Upvotes: 1
Reputation: 1766
It's normally. If you want to remove first empty element use next:
String input = "<a><b><c></c></b></a>";
String[] strs = input.split("[<>]+");
String[] s =Arrays.copyOfRange(strs, 1, strs.length);
Or go in cycle looking for empty elements.
Upvotes: 1
Reputation: 36767
If you're sure about the input format you can do:
String[] s =input.substring(1).split("[<>]+");
System.out.println(Arrays.toString(s));
Upvotes: 0
Reputation: 10067
You will need to iterate through all the elements to get rid of the empty ones..
Upvotes: 0