每x个数字后拆分字符串

时间:2018-05-20 13:44:28

标签: java string split numbers add

我有点问题。我需要将一个String拆分为x部分并将其添加到一起。编辑:如果我想用x = 3分割它,那么我将需要4个部分,但如果我想用x = 4分割它,那么我将需要3个部分。所以我的硬编码很糟糕。 而且我的x总是我的stringlength的倍数(3是009056737152的倍数 - 长度12)

示例:字符串:" 009056737152"和adresslength = 3;意味着我必须在3个数字之后拆分字符串。 009 056 737 152然后我必须从右到左一起添加:251 + 737 + 650 + 009 = 1647.我的代码实际上不能正常工作。还有其他想法吗?

    int adresslength = 3;
    String str = "009056737152"; System.out.println(str);
    String result = str.substring(0,str.length() - adresslength - adresslength - adresslength );
    String result2 = str.substring(adresslength,str.length() + -adresslength -adresslength );
    String result3 = str.substring(adresslength + adresslength,str.length() +  -adresslength  );
    String result4 = str.substring(adresslength + adresslength + adresslength,str.length()  );
    System.out.println(result); System.out.println(result2);System.out.println(result3);System.out.println(result4);

4 个答案:

答案 0 :(得分:1)

可能这段代码解决了你的问题......

    int adresslength = 3;
    String str = "009056737152";
    System.out.println(str);
    String[] result = new String[str.length() / adresslength];
    int j = 0;
    for (int i = 0; i < str.length();) {
        result[j] = str.substring(i, (i + adresslength));
        char[] aux = new char[adresslength];
        int k2 = 0;
        for (int k = aux.length - 1; k >= 0; k--) {
            if ((j == 0)) {
                aux[k2] = result[j].charAt(k2);
                k2++;
            }else{
                aux[k2] = result[j].charAt(k);
                k2++;
            }

        }
        result[j] = String.copyValueOf(aux);
        i = i + adresslength;
        System.out.println(result[j]);
        j++;
    }

    int aux = 0;
    for (String string : result) {
        aux += Integer.valueOf(string);
    }
    System.out.println(aux);

答案 1 :(得分:1)

如果您首先计算零件的尺寸,则可以轻松计算零件范围。

public static String[] partitionByCount(String input, int count) {
    if (count <= 0)
        throw new IllegalArgumentException("Invalid count: " + count);
    int size = input.length() / count;
    if (size * count != input.length())
        throw new IllegalArgumentException("String of length " + input.length() +
                " cannot be split into " + count + " equally-sized parts");
    String[] parts = new String[count];
    for (int i = 0; i < count; i++)
        parts[i] = input.substring(i * size, (i + 1) * size);
    return parts;
}

测试

System.out.println(Arrays.toString(partitionByCount("009056737152", 2)));
System.out.println(Arrays.toString(partitionByCount("009056737152", 3)));
System.out.println(Arrays.toString(partitionByCount("009056737152", 4)));

输出

[009056, 737152]
[0090, 5673, 7152]
[009, 056, 737, 152]

如果要指定分区大小而不是分区计数,则可以轻松调整代码:

public static String[] partitionBySize(String input, int size) {
    if (size <= 0)
        throw new IllegalArgumentException("Invalid size: " + size);
    int count = input.length() / size;
    if (count * size != input.length())
        throw new IllegalArgumentException("String of length " + input.length() +
                " cannot be split into equally-sized parts of length " + size);
    String[] parts = new String[count];
    for (int i = 0; i < count; i++)
        parts[i] = input.substring(i * size, (i + 1) * size);
    return parts;
}

测试

System.out.println(Arrays.toString(partitionBySize("009056737152", 2)));
System.out.println(Arrays.toString(partitionBySize("009056737152", 3)));
System.out.println(Arrays.toString(partitionBySize("009056737152", 4)));

输出

[00, 90, 56, 73, 71, 52]
[009, 056, 737, 152]
[0090, 5673, 7152]

然后将相反的部分相加,可以更容易地反转输入,对其进行分区,然后求和:

public static int sumReversedPartitions(String input, int size) {
    String reversed = new StringBuilder(input).reverse().toString();
    String[] parts = partitionBySize(reversed, size);
    return Arrays.stream(parts).mapToInt(Integer::parseInt).sum();
}

测试

System.out.println(sumReversedPartitions("009056737152", 2));
System.out.println(sumReversedPartitions("009056737152", 3));
System.out.println(sumReversedPartitions("009056737152", 4));

输出

153         // 25 + 17 + 37 + 65 + 09 + 00
2538        // 251 + 737 + 650 + 900
7182        // 2517 + 3765 + 0900

答案 2 :(得分:1)

    public static void main(String[] main){
         int adresslength = 3;
         String str = "009056737152"; System.out.println(str);
         int result = 0;
         for(int i=0;i<str.length();i+=adresslength){
             String subString = str.substring(i , i+adresslength);
             String nSString = new String();
             for(int j=subString.length()-1;j>=0;j--){
                 nSString += subString.charAt(j);
             }
             result+=Integer.valueOf(nSString);
         }
         System.out.println(result);
    }

您可以更改地址长度为2,4,5,并根据您的要求另外添加约束,例如地址长度将按字符串长度等等。

答案 3 :(得分:0)

这是使用Java 8流的较短版本。

public static int splitNumber(String s, int length) {
    s = new StringBuilder(s).reverse().toString();
    return IntStream.iterate(0, i -> i + length)
            .mapToObj(start -> s.substring(start, start + length))   // Take the i-th substring
            .mapToInt(Integer::parseInt)                             // Turn into int
            .limit(s.length() / length)                              // Limit the number of parts
            .sum();
}