HOWTO: Split a string using split

Recently I was faced with a problem... I had an output of a 'ps' command and I needed to get the pid out. I know that you can get it out using awk and grep, but I could not get it to work from within my Java program. So, I had my output in a string array, where each line was a separate process. If you have seen an output of the ps command, you know that there is non-constant number of spaces between each field. So, to overcome that, I had to resort to regular expressions.

Here's the code that worked for me.

String str = "32944 s000  R+     0:00.00 ps x";
String[] parts = str.split("[ ]+");

Hope this helps!