简体   繁体   中英

I am trying to insert a string character to another string. How can I achieve it in java?

This is the code I am working upon. I dont know where I am going wrong.

package mcdcpairwise;
import java.io.*;
import java.util.*;

public class Permutation
{
    public static void main(String[] args)
    {
        String a="000";
        String b="|&";

        for (int i=0; i < a.length(); i++){
            if (i % 2 != 0){
                a = a.substring(0,i-1) + b.substring(0,i-1). + a.substring(i, a.length()) + b.substring(i, b.length());
                System.out.println(a);
            }
        }

    }
}    

The error I am facing is:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2 at java.lang.String.substring(String.java:1967) at mcdcpairwise.Permutation.main(Permutation.java:13)

The output should be :

0|0&0

正确的代码应该是a.substring(0,i)

It isn't clear from your question exactly what your "rules" are for processing this. However, your output seems to simply insert a character between each character of your source a string.

Instead of using a substring, create a separate StringBuilder to add individual characters to. The code below produces the output you are looking for:

String string = "000";
StringBuilder output = new StringBuilder();

for (int i = 0; i < string.length(); i++) {

    // Get current character in the string
    char c = string.charAt(i);

    // Add the current character to the output
    output.append(c);

    // If more characters exist, add the pipe
    if (i != string.length() - 1) {
        output.append("|");
    }
}

System.out.println(output.toString());

You can use String.toCharArray to get a char[] from a String . That way we can iterate more easily both String using an index.

String a="000";
String b="|&";

char[] arrayA = a.toCharArray();
char[] arrayB = b.toCharArray();

Then, all we have to do is to merge two array (from String s) taking one character from both. Adding two conditions (one per array) to prevent any ArrayIndexOutOfBOundsException, we can insure we will merge two arrays.

StringBuilder sb = new StringBuilder();

//Add a char from both array (until we reach on of the limit)
int i = 0;
while( i < arrayA.length && i < arrayB.length){
    sb.append(arrayA[i]).append(arrayB[i]);
    ++i;
}

Then we just need to add the remaining characters using a for loop on both arrays. Only one of those loop will be triggered (or none) since at least one previous condition ( i < arrayA.length && i < arrayB.length ) is already false .

//Add the rest of `a` if any
for(int j = i; j < arrayA.length; ++j){
    sb.append(arrayA[j]);
}

//Add the rest of `b` if any
for(int j = i; j < arrayB.length; ++j){
    sb.append(arrayB[j]);
}

System.out.println(sb.toString());

0|0&0

Here's a one line solution:

System.out.println((a + b).replaceAll("(?<=.)(?=.{" + (a.length() - 1) + "}(.))|.(?=.{0," + (b.length() - 1) + "}$)", "$1"));

This works with all combinations of non-blank starting strings.

See live demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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