Java String split by comma with white space

Description

Java String split by comma with white space

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    String firstString = "1, 2, 3, 4, 5, 6, 7, 8";

    System.out.printf("%s\n", firstString);

    String[] results = firstString.split(",\\s*"); // split on commas
    System.out.println(Arrays.toString(results)); // display results

  }//ww  w  .j a  v a 2 s . c o  m
}

The following table lists the predefined Java Regular expression character classes:

CharacterMatches
\d any digit
\D any non-digit
\w any word character
\W any nonword character
\s any white-space character
\S any non-whitespace character



PreviousNext

Related