Home > Blockchain >  How can I split this line into an Array without having an empty element?
How can I split this line into an Array without having an empty element?

Time:12-02

Here is the line

String text = " !!!.. first, second: third";

Here is my method

  public static String[] splitWords(String text) {
        if (text == null ) return null;
        if(text.isBlank() || text.isEmpty()) return null;
        if(text.matches("[.;,:?!\\s] ")) return null;

         String[] splitRaw = text.trim().split("[.;,:?!\\s] ");

        System.out.println(Arrays.toString(splitRaw));
        return splitText;
    }

Here is the result: "[, first, second, third]"

As you can see, one empty element is in the start of the array. Array length should be 3, but it's 4.

CodePudding user response:

Your regex is fine and the empty string is unavoidable in this case, if you want to make sure there aren't any in the result, you can always stream, filter and recollect it:

public static String[] splitWords(String text) {
    if (text == null ) return null;
    if(text.isBlank() || text.isEmpty()) return null;
    if(text.matches("[.;,:?!\\s] ")) return null;
    return Arrays.stream(text.trim().split("[.;,:?!\\s] "))
        .filter(s -> !s.equals("")).toArray(String[]::new);
}

CodePudding user response:

Call split(regex, -1); to remove null parts

  •  Tags:  
  • java
  • Related