g.dev
g.dev

Reputation: 9

split up data with java split() make empty data of first array

DB has like this data for hashtag in string type

#a#b#c#d#e

with this data, I want split each tag

so I code this

 String tagToArray[] = output.getText("TAG", i).split("#");

but the result is

tagToArray[, a, b, c, d, e]

I want to remove first empty data what is the right way to splay data of hashtag?

Upvotes: 0

Views: 61

Answers (3)

RICK XU
RICK XU

Reputation: 1

The issue can be solved with the regex (?<=\#)(.)

Code:

output.getText("TAG", i).split("(?<=\#)(.)");

Upvotes: 0

Eklavya
Eklavya

Reputation: 18440

This extra empty string produce for first #.

So, you can replace the first character if it is # then split.

 String tagToArray[] = output.getText("TAG", i).replaceFirst("^#", "").split("#");

Upvotes: 1

Joakim Danielson
Joakim Danielson

Reputation: 51973

Remove the first # before splitting

 String[] array = str.replaceFirst("#", "").split("#");

Upvotes: 1

Related Questions