Home > Software design >  finding String.split() length
finding String.split() length

Time:10-02

I have a question when it comes to string.split() and finding length for a particular example.

Say for example I have a date of 7/10/2014 and an increment value of 365 next to the date with a couple of spaces like so.

 7/10/2014  365 

I want the length of the string to be 4 as it takes the month, day, year and increment value. I can get the first 3 length values of date easily by doing this:

String[] content = date.split("/").length;

But I cannot remember how to include the increment value of 365 to get my length of 4. I'm assuming some regex is in order, but not sure how to implement it.

CodePudding user response:

Yes split also accepts regex.

String[] content = date.split("/|\\s ");

meaning of regex: split on / or(|) any whitespace character(\s) " " matches the previous token between one and unlimited times so any number of whitespaces will be matched

  •  Tags:  
  • java
  • Related