ovnia
ovnia

Reputation: 2500

Split string by space, Java

Here is a string:

String s = "119 days 6 hours 13 minutes 24 seconds";

How make a split for getting array such as this:

{
  "119 days",
  "6 hours",
  "13 minutes",
  "24 seconds"
}

I've no idea how to make it myself.

I looked for String.split() and tried [a-z]\s regExp, but it cuts the last character

Upvotes: 1

Views: 6690

Answers (5)

Lee Fogg
Lee Fogg

Reputation: 795

Theres two solutions to what you're trying to to, firstly you can simply split the string with
s.split(" "); however that will return each word and ease all the spaces.
So the two solutions would be to split by spaces and go through the array and add each two words together with a space in-between.
Or you could change the input string so you're splitting on something other than spaces such as commas (though this would involve also editing where the string came from into the new format):

String s = "119 days, 6 hours, 13 minutes, 24 seconds";
String[] parts = s.split(", ");

Upvotes: 2

hwnd
hwnd

Reputation: 70722

One way for this specific case, split using a negative lookbehind.

import java.util.Arrays;

class rTest {
  public static void main (String[] args) {
    String s = "119 days 6 hours 13 minutes 24 seconds";
    String[] parts = s.split("(?<![^a-zA-Z])\\s");
    System.out.println(Arrays.toString(parts));
  }
}

Regular expression:

(?<!           look behind to see if there is not:
 [^a-zA-Z]     any character except: 'a' to 'z', 'A' to 'Z'
)              end of look-behind
\s             whitespace (\n, \r, \t, \f, and " ")

Ouput

[119 days, 6 hours, 13 minutes, 24 seconds]

Upvotes: 4

Mike Clark
Mike Clark

Reputation: 10136

You could use split with zero-width lookaround, but I prefer to avoid lookaround when it is reasonable to do so. Using Matcher makes the procedural code more verbose, but it has the added advantage of making the regex easier to understand. Since most programmers understand the procedural language better than regex, this sometimes results in more maintainable code.

String s = "119 days 6 hours 13 minutes 24 seconds";
Pattern regex = Pattern.compile("\\w+ \\w+"); // match two words
Matcher matcher = regex.matcher(s);
ArrayList<String> list = new ArrayList<String>();
while (matcher.find()) {
    list.add(matcher.group());
}
System.out.println("list=" + list);

Upvotes: 2

Paul Wagland
Paul Wagland

Reputation: 29116

You are in the right path, but you need to use zero-width matcher groups so that they don't get swallowed, so something like:

input.split(" (?=[0-9])");

Upvotes: 3

Pshemo
Pshemo

Reputation: 124215

You can split on every space that has digit after it. To do this you can use look ahead mechanism
(?=nextElement) since split uses regex. To represent any whitespace in regex you can use "\\s" and to represent any digit "\\d".

Upvotes: 2

Related Questions