0

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");  
4
  • Hint: split takes a regex. Commented Feb 6, 2014 at 13:32
  • I edited my question. Do you have any suggestion? Commented Feb 6, 2014 at 13:35
  • 1
    String.replace() followed by .split()? Commented Feb 6, 2014 at 13:35
  • Hint #2 - txt2re.com generates Java code Commented Feb 6, 2014 at 13:35

2 Answers 2

3

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

bolunmus[i]=line.split(" *(:|kB) *");  
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.