AloneInTheDark
AloneInTheDark

Reputation: 938

Java string split by multiple character delimiter

I have a project in Java, which collects data from a Linux machine and gets output like this:

SwapCached: 0 kB

I need to split this sentence into this words:

SwapCached,0

I know how to split by single characters, but don't know how to split with multiple words(like kB) and multiple spaces together.

EDIT: I tried this one but it didn't clear the spaces.

bolunmus[i]=line.split(":|\\ +|\\ |kB");  

Upvotes: 0

Views: 1109

Answers (2)

Karol S
Karol S

Reputation: 9457

It doesn't clear spaces, because you didn't tell it to:

bolunmus[i]=line.split(" *(:|kB) *");  

Upvotes: 3

user1983983
user1983983

Reputation: 4841

Try this:

line.split(":\\s+|\\skB"); 

Upvotes: 1

Related Questions