Java String Array Merge mergeStrings(String[] str1, String str1Tag, String[] str2)

Here you can find the source of mergeStrings(String[] str1, String str1Tag, String[] str2)

Description

This function will take a string array in str1, and it will insert the string array in str2 into it, with the first line of str2 being inserted where the str1Tag is located in str1.

License

Open Source License

Parameter

Parameter Description
str1 : String array 1
str1Tag : The tag in str1 that marks where str2 should be inserted
str2 : String array 2

Declaration

private static String[] mergeStrings(String[] str1, String str1Tag, String[] str2) 

Method Source Code

//package com.java2s;
/*//w w w. j  a  v  a2  s  .c  o m
 *  Copyright Matthew J.H. Millard 2008
 * 
 * SolvereUtilities.java is part of Solvere4D.
    
Solvere4D is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
Solvere4D is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Solvere4D.  If not, see <http://www.gnu.org/licenses/>.
    
 */

public class Main {
    /**
     * This function will take a string array in str1, and it will insert the 
     * string array in str2 into it, with the first line of str2 being inserted
     * where the str1Tag is located in str1.
     * 
     * @param str1    : String array 1
     * @param str1Tag : The tag in str1 that marks where str2 should be inserted
     * @param str2    : String array 2
     */
    private static String[] mergeStrings(String[] str1, String str1Tag, String[] str2) {
        String[] finalStr = new String[str1.length + str2.length - 1];
        int fCtr = 0;
        int s = 0;
        for (int i = 0; i < str1.length; i++) {
            s = str1[i].indexOf(str1Tag);
            if (s != -1) {
                finalStr[fCtr] = str1[i].substring(0, s) + " " + str2[0];
                fCtr++;
                for (int j = 1; j < str2.length; j++) {
                    finalStr[fCtr] = str2[j];
                    fCtr++;
                }

            } else {
                finalStr[fCtr] = str1[i];
                fCtr++;
            }

        }

        return finalStr;
    }
}

Related

  1. mergeJoin(String sep, String[] a1, String[] a2)
  2. mergeString(String currentValue, String newValue)
  3. mergeString(String[] str1, String[] str2)
  4. mergeStrings(Object... strings)
  5. mergeStrings(String s1, String s2)
  6. mergeStringsFormatted(String[] strings, String spacer)