在java中合并多个字符串

时间:2015-09-21 14:28:50

标签: java string

我有一个任务,我被要求创建一个合并多个字符串的方法,以便第一个字符串中的第一个字符后跟第二个字符串中的第一个字符,后跟第三个字符串中的第一个字符,所以上。

public static String merge(String... s)

如果我merge("AH", "HI", "U"),结果将是AHUHI。当字符串的数量未知时,我不太确定如何处理这个问题,有人知道如何执行这个问题吗?¨

这就是我的尝试:

public static String merge(String... s)
    {
        StringBuilder b = new StringBuilder();

         for(int i = 0; i < s.length ; i++)
         {
             for(int y = 0; y < s.length ; y++)
             {
             b.append(s[y].charAt(i));
             }
         }
         return b.toString();
    }

这是我得到的例外:

  

线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

5 个答案:

答案 0 :(得分:2)

  • 查找User::chunk(200, function($users) { foreach ($users as $user) { // } }); (参数中最长php -d memory_limit=-1 composer update 的长度)并将外循环更改为迭代maxLength

  • 在通过String访问maxLength的字符之前,检查String是否足够长,以便您不会获得charAt()

由于您已经在处理多个输入字符串,因此代码在这些更改后应该可以正常工作。

String

答案 1 :(得分:1)

你是在正确的行,但你需要检查你引用的每个字符串是否足够大,不会抛出StringIndexOutOfBoundsException。首先,尝试获取最大字符串长度:

public static String merge(String... s)
{

    int maxLen = 0;
    for (String str : s) // this loops through each string in the array s
    {
        maxLen = Math.max(maxLen, str.length());
    }

    // maxLen is now the length of the longest string;

    StringBuilder b = new StringBuilder();

    for (int i = 0; i < maxLen; ++i) // for each character position up to the max...
    {
         for (String str : s) // loop through each string:
         {
             if (str.length() > i) // check whether current string has any characters left
             {
                 b.append(str.charAt(i));
             }
         }
     }
     return b.toString();
}

答案 2 :(得分:1)

多数民众赞成我会这样做: 基本上,你循环每String并始终取String的第一,第二,第三......字符并将其附加到StringBuilder

private static String merge(String... strings) {
    StringBuilder sb = new StringBuilder();
    int adv;
    boolean edited;

    adv = 0;
    edited = true;
    while (edited) {
        edited = false;
        for (String s : strings) {
            if (adv < s.length()) {
                sb.append(s.charAt(adv));
                edited = true;
            }
        }
        adv++;
    }
    return sb.toString();
}

答案 3 :(得分:0)

我就是这样做的:

public static String merge(String... s)
{
    // Here we create a StringBuilder, this will store our result string
    StringBuilder b = new StringBuilder();

    // This boolean will control when we stop appending
    boolean appended = true;

    // The outer loop will loop over the indices of the available characters
    // until we have no more characters to append
    for (int i = 0; appended; ++i) {
        // We have to default this to false to start our loop so that we don't
        // exit early
        appended = false;
        // Loop over the individual strings that we were passed
        for (String item : s) {
            // If the string we are looking at has a character at the current
            // position, we append it to our StringBuilder, otherwise we skip
            // it
            if (i < item.length()) {
                b.append(item.charAt(i));
                // Because we appeneded a character, we might have additional
                // characters, so set this so that our loop continues
                appended = true;
            }
        }
    }
    // Now we call the toString() method of StringBuilder to get a String
    // result, which we return to our caller.
    return b.toString();
}

答案 4 :(得分:-2)

将每个String拆分为一个数组。与此类似:Split string into array of character strings

然后遍历数组并取出索引0并将其分配给新字符串,并为索引1执行此操作,依此类推。