Question

I want to split the input String values separated by one of these char

,
;
blankspace
\
/

in a double array

My code for , is

String valueS[] = input.split(",");
        int n = valueS.length;
        double yvalue[] = new double[n];
        double sum = 0;
        for (int i = 0; i < n; i++) {
            yvalue[i] = Double.parseDouble(valueS[i]);
        }

What is the right syntax that I should put in

String valueS[] = input.split(",");

to split also with the others listed chars?

Was it helpful?

Solution

To actually answer your question, String.split (String regularExpression) uses a regular expression to determine what to split the array by.

This regular expression below will try to split the array by any one of these characters:

  • ,
  • ;
  • space
  • \
  • /

It is using an "Enumeration" that will match 1 or more of these characters in a row, and then split on them:

String[] valueS = input.split("[,;\\s\\\\/]+");

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top