简体   繁体   English

如何使用Java按小时:分钟:秒分割字符串

[英]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 19Timesplit[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.

相关问题 如何将整数拆分为字符串并按秒/分钟/小时自动转换 - How to split an integer into String and Convert it automatically by seconds/minutes/hours Java如何仅使用小时,分钟和秒来比较LocalTime - Java how to compare LocalTime using only hours, minutes and seconds 使用Java的转换器从数秒到数天,数小时,数分钟和数秒的问题 - Problems with a converter from seconds to day, hours, minutes, and seconds using Java Java:将秒的输入转换为小时/分钟/秒 - Java: Converting an input of seconds into hours/minutes/seconds 在 Java 中使用 Duration 对象失败将天、分钟、小时转换为秒 - Convert days, minutes, hours to seconds in Java Using Duration object failing 如何在小时,分钟和秒的时间内将字符串转换为Date对象 - How to convert a String to a Date object for hours, minutes, and seconds 如何使用JSpinner分别更改小时,分钟和秒? - How to change hours, minutes, and seconds separately using a JSpinner? Java代码将秒转换为分钟,小时和天 - Java Code to convert seconds into minutes, hours and days 向java SQL Date添加小时,分钟和秒 - Adding hours, minutes and seconds to java SQL Date 如何将自纪元以来的秒数转换为Java中的小时/分钟/秒? - How can I convert seconds since the epoch to hours/minutes/seconds in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM