简体   繁体   English

查找 String.split() 长度

[英]finding String.split() length

I have a question when it comes to string.split() and finding length for a particular example.关于 string.split() 和查找特定示例的长度,我有一个问题。

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,有几个空格。

 7/10/2014  365 

I want the length of the string to be 4 as it takes the month, day, year and increment value.我希望字符串的长度为 4,因为它需要月、日、年和增量值。 I can get the first 3 length values of date easily by doing this:通过这样做,我可以轻松获得日期的前 3 个长度值:

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.但我不记得如何包含 365 的增量值以获得 4 的长度。我假设一些正则表达式是有序的,但不确定如何实现它。

Yes split also accepts regex.是的split也接受正则表达式。

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正则表达式的含义:在 / 或 (|) 上拆分任何空白字符 (\s) “+” 与前一个标记匹配一次到无限次,因此将匹配任意数量的空白

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM