如何使用Java将字符串拆分小时:分钟:秒

时间:2014-10-18 11:21:10

标签: java

我的字符串格式如下:

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"

其中Text只是普通文本而11:54:02是时间。现在时间不断变化,所以我们可以有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"]

这样我就可以读取第一个和第二个文本值。我尝试使用以下拆分,但它似乎不起作用:

String[] Timesplit = fileContent.split("0-9:0-9:0-9");

理想情况下,我希望Timesplit[0][1]"Fri Sep 19Timesplit[2]拥有+"RT @OliverBullough: Andrei Yakunin, investor son of Putin's friend Vladimir, on Russian deals: \"2014 is lost, 2015 is effectively lost.\" ht\u2026"

是否可以拆分数据?

4 个答案:

答案 0 :(得分:1)

您可以将字符串拆分为":"

String[] split = fileContent.split(":");

所以" 11:54:02"将返回[" 11"," 54"," 02"]

" 0-9:0-9:0-9"似乎不是正则表达式,这意味着fileContentSplit("0-9:0-9:0-9")实际上会尝试找到" 0-9:0-9:0-9"在字符串中并将其拆分。

答案 1 :(得分:1)

尝试编写[0-9]而不是0-9。

答案 2 :(得分:1)

使用这个, String [] split = fileContent.split(" [0-9] [0-9]:[0-9] [0-9]:[0-9] [0-9]") ;

感谢。

答案 3 :(得分:1)

我使用了这个正则表达式

([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]);

输出>>

[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"