Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java/a更好的代码将字符串拆分为多个片段_Java_Regex - Fatal编程技术网

使用java/a更好的代码将字符串拆分为多个片段

使用java/a更好的代码将字符串拆分为多个片段,java,regex,Java,Regex,我必须拆分此字符串: 00016282000079116050 它有预定义的块。应该是这样的: 00016 282 00 0079 116 050 我制定了以下代码: String unformatted = String.valueOf("00016282000079116050"); String str1 = unformatted.substring(0,5); String str2 = unformatted.substring(5,8); String str3 = unfo

我必须拆分此字符串:

00016282000079116050
它有预定义的块。应该是这样的:

00016 282 00 0079 116 050
我制定了以下代码:

String unformatted = String.valueOf("00016282000079116050");
String str1 = unformatted.substring(0,5); 
String str2 = unformatted.substring(5,8);
String str3 = unformatted.substring(8,10);
String str4 = unformatted.substring(10,14);
String str5 = unformatted.substring(14,17);
String str6 = unformatted.substring(17,20);

System.out.println(String.format("%s %s %s %s %s %s", str1, str2, str3, str4, str5, str6));
这是可行的,但我需要使这段代码更美观


使用Java8流或正则表达式的东西应该是好的。有什么建议吗?

您可以使用正则表达式编译一个包含6个组的
模式,如

String unformatted = "00016282000079116050"; // <-- no need for String.valueOf
Pattern p = Pattern.compile("(\\d{5})(\\d{3})(\\d{2})(\\d{4})(\\d{3})(\\d{3})");
Matcher m = p.matcher(unformatted);
if (m.matches()) {
    System.out.printf("%s %s %s %s %s %s", m.group(1), m.group(2), m.group(3), 
            m.group(4), m.group(5), m.group(6));
}
,如注释中所指出的,您可以使用
Matcher.replaceAll(String)

if (m.matches()) {
    System.out.println(m.replaceAll("$1 $2 $3 $4 $5 $6"));
}
一种更有针对性的方法是使用一个循环和一个预定义长度的数组

int[] lims = {5,3,2,4,3,3};
int total = 0;
String original = "00016282000079116050";

for (int i = 0 ; i < lims.length ; i++) {
    System.out.print(original.substring(total, total+=lims[i]) + " ");
}

溪流=为什么/如何?Regex=如果您知道要提取的字符串值的模式,则可能是这样。基本上,对字符串进行切片没有什么错。即使写起来有点困难,使用子字符串也不是一个坏方法。您是否只是使用了
字符串.valueOf
使其对其他对象更灵活?因为您选择使用正则表达式,为什么不执行替换而不是使用格式化字符串?@ElliottFrisch感谢您的帮助。这正是我想要的。@Casimirithippolyte这是另一个不错的选择;谢谢当然,如果这是一次性操作,则不需要显式地处理
模式
匹配器
,一个简单的
System.out.println(未格式化的.replaceFirst(\\d{5})(\\d{3})(\\d{2})(\\d{4})(\\d{3})(\\d{3})(\\d{3}),“$1$2$3$4$6”)可以…
int[] lims = {5,3,2,4,3,3};
int total = 0;
String original = "00016282000079116050";

for (int i = 0 ; i < lims.length ; i++) {
    System.out.print(original.substring(total, total+=lims[i]) + " ");
}
00016 282 00 0079 116 050 
        String unformatted = "00016282000079116050";
        int[] splittingPoints = new int[]{5,3,2,4,3,3};
        int charactersProcessed = 0;
        int howManyBeforeSplit = 0;
        int chunksDone = 0;
        for (char c : unformatted.toCharArray()) {
            if ((charactersProcessed++ - chunksDone) == splittingPoints[howManyBeforeSplit]) {
                System.out.print(" ");
                chunksDone += splittingPoints[howManyBeforeSplit++];
            }
            System.out.print(c);
        }