使用java拆分字符串

时间:2012-04-11 11:36:16

标签: java

我想以相同的顺序多次拆分包含日期,时间和一些注释的非常长的字符串,如何使用java分别将它们分成日期,时间和注释。

例如:

21 March 2012 3:48 PM 11 doz 30 September 2011 10:51 AM Hotlist Task Deleted: To-do Ring re: Boody (Jessica ordered socks)With: 3 Beaches Chemist Deleted by: MASTER 21 September 2011 12:55 PM Hotlist Task Created: To-do Ring re: Boody (Jessica ordered socks) With: 3 Beaches Chemist Scheduled by: MASTER Assigned to: MASTER 21 September 2011 12:55 PM 7 doz

我希望输出为:

Date              Time     Comments
21 March 2012     3:48 PM  11 doz
30 September 2011 10:51 AM Hotlist Task Deleted: To-do Ring re: Boody (Jessica ordered socks)With: 3 Beaches Chemist Deleted by: MASTER
21 September 2011 12:55 PM Hotlist Task Created: To-do Ring re: Boody (Jessica ordered socks) With: 3 Beaches Chemist Scheduled by: MASTER Assigned to: MASTER
21 September 2011 12:55 PM 7 doz

4 个答案:

答案 0 :(得分:0)

您可以尝试使用正则表达式,但必须在条目之间使用行/条目分隔符。

答案 1 :(得分:0)

我建议创建一个算法,根据您的已知格式查找值日期字符串(例如,查找与其中一个月匹配的下一个字符串)并根据该字符串中断字符串。找到这个月,回到你发现的那一天并分开。

答案 2 :(得分:0)

我还建议使用正则表达式来匹配你想要破坏字符串的条件。

此外,您可以使用分隔符让正则表达式表达式破坏字符串。

答案 3 :(得分:0)

s.replaceAll ("([0-3]?[0-9] [A-Z][a-z]+ 20[0-9][0-9] )", "\n$1")

结果:

21 March 2012 3:48 PM 11 doz 
30 September 2011 10:51 AM Hotlist Task Deleted: To-do Ring re: Boody (Jessica ordered socks)With: 3 Beaches Chemist Deleted by: MASTER 
21 September 2011 12:55 PM Hotlist Task Created: To-do Ring re: Boody (Jessica ordered socks) With: 3 Beaches Chemist Scheduled by: MASTER Assigned to: MASTER 
21 September 2011 12:55 PM 7 doz