Calculate values ​​from a string

How would you analyze the values ​​in a string, for example below?

12:40:11  8    5                  87

The difference between the numbers changes, and the first value is time. The following regular expression does not separate the time component:

str.split("\\w.([:]).")

Any suggestions?

+5
source share
2 answers

A regular expression \s+matches one or more spaces, so it will be splitin 4 values:

"12:40:11", "8", "5", "87"

Like a Java string literal, this template "\\s+".

If you want to get all 6 numbers, you also want to split into :, therefore, a pattern \s+|:. As a Java string literal, this is "\\s+|:".

References


Scanner

String.split java.util.Scanner useDelimiter , split. , int nextInt(), int ( , ).

+8

doc String API.

str.split("\\s+");

[ '12:40:11', '8', '5', '87' ]

str.split("\\s+|:");

[ '12', '40', '11', '8', '5', '87' ]
+2

All Articles