简体   繁体   English

如何在Java中将字符串拆分为不同大小的部分?

[英]How do I split a string in Java to different size parts?

I have a timestamp: 200212312359 How can I split this to 2012.12.31.23.59 我有一个时间戳: 200212312359如何将其拆分为2012.12.31.23.59

The easy way to do is .split("(?<=\\\\G.{2})") , then combine the first 2 element of the array, but I was wondering if there is any, more professional solution for this. 最简单的方法是.split("(?<=\\\\G.{2})") ,然后合并数组的前2个元素,但是我想知道是否有任何更专业的解决方案。

You can get the same result with a single replaceAll call and a somewhat overly complex regex. 您可以通过一个replaceAll调用和一个过于复杂的正则表达式来获得相同的结果。

"200212312359".replaceAll("(^\\d{4}|\\d{2})(?!$)", "$1.")

Broken down, it matches 4-digits at the start ^\\\\d{4} , or 2-digits \\\\d{2} anywhere else, with a negative lookahead on the end of input (?!$) to avoid matching the last pair. 细分后,它会匹配开头^\\\\d{4}位数字或其他任何位置的2位数字\\\\d{2} ,并在输入的末尾(?!$)前面使用负号来避免匹配最后一对。 It then replaces the 4 or 2 digits with the a dot concatenated to the digits. 然后,将4或2位数字替换为与该数字连接的点。

There is nothing necessarily unprofessional about your solution; 您的解决方案不一定没有专业知识; it just isn't intuitive. 这只是不直观。 If you comment it with a simple explanation of what date format it splits, then that's as professional a solution as any other, IMHO. 如果您以简单的解释拆分日期格式来评论它,那么IMHO和其他任何解决方案一样都是专业的解决方案。

Also, If you know the position of each value (dot separated), you could use substring method: 另外,如果您知道每个值的位置(点分隔),则可以使用子字符串方法:

 String unformatted = String.valueOf(200212312359);
 String year = unformatted.substring(0,4); 
 String month = unformatted.substring(4,6);
 String day = unformatted.substring(6,8); 
 String hour = unformatted.substring(8,10);
 String minute = unformatted.substring(10,12);
 String formatted = String.format("%s.%s.%s.%s.%s", year, month, day, hour, minute);

Please read http://docs.oracle.com/javase/7/docs/api/java/lang/String.html 请阅读http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

As Colleen pointed in comment you can use groups to create new formated date. 正如Colleen在评论中指出的那样,您可以使用组来创建新的格式化日期。 Here is example 这是例子

Pattern p = Pattern.compile("^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$");
Matcher m = p.matcher("200212312359");
StringBuilder sb = new StringBuilder();
if (m.find()) {
    sb.append(m.group(1));
    for (int i = 2; i <= m.groupCount(); i++)
        sb.append(".").append(m.group(i));
}
System.out.println(sb);

output: 2002.12.31.23.59 输出: 2002.12.31.23.59

You can also use named-groups to name every group and use them instead of their numbers: 您也可以使用named-groups命名每个组,并使用它们代替数字:

Pattern p2 = Pattern.compile("^(?<year>\\d{4})(?<month>\\d{2})" +
        "(?<day>\\d{2})(?<hour>\\d{2})(?<minute>\\d{2})$");
Matcher m2 = p2.matcher("200212312359");
if (m2.find()) {
    String formatedDate = m2.group("year") + "." + m2.group("month")
            + "." + m2.group("day") + "." + m2.group("hour") 
            + "." + m2.group("minute");
    System.out.println(formatedDate);
}

You can also update your split to prevent split on first two digits like this 您还可以更新拆分,以防止像这样的前两位数字拆分

"200212312359".split("(?<=^\\d{4}|(?!^)\\G.{2})"

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

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