[英]How to split a string by hours:minutes:seconds using Java
I've got strings that have the following format: 我有以下格式的字符串:
Text 11:54:02 Text
[1]"Fri Sep 19 11:07:24 +"RT @OliverBullough: Andrei Yakunin, investor son of Putin's friend Vladimir, on Russian deals: \"2014 is lost, 2015 is effectively lost.\" ht\u2026"
Where Text is just normal text and the 11:54:02
is the time. 文本只是普通文本,而11:54:02
是时间。 Now the time varies constantly, so we could have 11:53:11 or 02:01:01 etc. I would like to know if it is possible to split by this time ie 现在时间在不断变化,所以我们可能会有11:53:11或02:01:01等。我想知道是否有可能在这个时间之前分开
[Text][Time][Text]
[[1]"Fri Sep 19] [11:07:24][ +"RT @OliverBullough: Andrei Yakunin, investor son of Putin's friend Vladimir, on Russian deals: \"2014 is lost, 2015 is effectively lost.\" ht\u2026"]
So that I can just read the first and second text values. 这样我就可以读取第一和第二文本值。 I've tried using the following split but it doesn't seem to work: 我尝试使用以下拆分,但似乎不起作用:
String[] Timesplit = fileContent.split("0-9:0-9:0-9");
Ideally I would want Timesplit[0]
to have [1]"Fri Sep 19
and Timesplit[2]
to have +"RT @OliverBullough: Andrei Yakunin, investor son of Putin's friend Vladimir, on Russian deals: \\"2014 is lost, 2015 is effectively lost.\\" ht\…"
理想情况下,我希望Timesplit[0]
拥有[1]"Fri Sep 19
, Timesplit[2]
具有+"RT @OliverBullough: Andrei Yakunin, investor son of Putin's friend Vladimir, on Russian deals: \\"2014 is lost, 2015 is effectively lost.\\" ht\…"
Is it possible to split the data? 是否可以分割数据?
You can just split the string by ":" 您可以将字符串用“:”分隔
String[] split = fileContent.split(":");
So "11:54:02" will return ["11", "54", "02"] 因此,“ 11:54:02”将返回[“ 11”,“ 54”,“ 02”]
"0-9:0-9:0-9" doesn't seem to be a regex, which means fileContentSplit("0-9:0-9:0-9")
will actually try to find "0-9:0-9:0-9" within the string and split it by that. “ 0-9:0-9:0-9”似乎不是正则表达式,这意味着fileContentSplit("0-9:0-9:0-9")
实际上会尝试查找“ 0-9:字符串中的“ 0-9:0-9”,然后将其拆分。
尝试写[0-9]而不是0-9。
Use this, String[] split = fileContent.split("[0-9][0-9]:[0-9][0-9]:[0-9][0-9]"); 使用它,String [] split = fileContent.split(“ [0-9] [0-9]:[0-9] [0-9]:[0-9] [0-9]”);
Thanks. 谢谢。
i used this regex 我用过这个正则表达式
([0-9]{2}):([0-9]{2}):([0-9]{2})
. 。
String fileContent="[1]\"Fri Sep 19 11:07:24 +\"RT @OliverBullough: Andrei Yakunin, investor son of Putin's friend Vladimir, on Russian deals: \\\"2014 is lost, 2015 is effectively lost.\\\" ht\\u2026\"";
String[] Timesplit = fileContent.split("([0-9]{2}):([0-9]{2}):([0-9]{2})");
System.out.println(Timesplit[0]);
System.out.println(Timesplit[1]);
output>> 输出>>
[1]"Fri Sep 19
+"RT @OliverBullough: Andrei Yakunin, investor son of Putin's friend Vladimir, on Russian deals: \"2014 is lost, 2015 is effectively lost.\" ht\u2026"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.