Java String Index Of mergeStringLines(String lineOne, String lineTwo, int keyIndex, int insertingIndex)

Here you can find the source of mergeStringLines(String lineOne, String lineTwo, int keyIndex, int insertingIndex)

Description

merge String Lines

License

Open Source License

Declaration

static public String mergeStringLines(String lineOne, String lineTwo,
            int keyIndex, int insertingIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    static public String mergeStringLines(String lineOne, String lineTwo,
            int keyIndex, int insertingIndex) {
        String mergedLine = "";
        String[] splittingLine = lineOne.split(",");
        String lineTwoWOKey = removeColumn(lineTwo, keyIndex);

        if (splittingLine.length <= insertingIndex)
            mergedLine = lineOne + "," + lineTwoWOKey;
        else {/*from w w  w.j a  v a 2  s .  co m*/
            mergedLine = getMergedLine(lineOne, lineTwoWOKey,
                    insertingIndex);
        }

        return mergedLine;
    }

    static String removeColumn(String line, int index) {

        ArrayList<String> splittingLine = new ArrayList<String>(
                Arrays.asList(line.split(",")));

        splittingLine.remove(index);

        return strArrayListToString(splittingLine);
    }

    static void removeColumn(ArrayList<String> lines, int index) {
        for (int i = 0; i < lines.size(); i++) {
            lines.set(i, removeColumn(lines.get(i), index));
        }
    }

    static String getMergedLine(String lineOne, String lineTwo,
            int insertingIndex) {
        ArrayList<String> lstLineOne = new ArrayList<String>(
                Arrays.asList(lineOne.split(",")));
        ArrayList<String> lstLineTwo = new ArrayList<String>(
                Arrays.asList(lineTwo.split(",")));

        for (int i = 0; i < lstLineTwo.size(); i++) {
            lstLineOne.add(insertingIndex + i, lstLineTwo.get(i));
        }

        return strArrayListToString(lstLineOne);
    }

    static String strArrayListToString(ArrayList<String> lstLine) {
        String line = "";

        for (int i = 0; i < lstLine.size(); i++) {
            line = line + lstLine.get(i);

            if (i != lstLine.size() - 1)
                line = line + ",";
        }

        return line;
    }
}

Related

  1. indexOfMultiple(String line, char character, int count, int startIndex)
  2. indexOfWord(String word, String string)
  3. isPhaseGroupContainsPhase(int index, String phaseName)
  4. lastIndexOf(String pattern, String s)
  5. lastIndexOfIgnoreCase(final String s, final String subS)
  6. readColumn(int columnIndex, String inputString, String columnSeparator)
  7. removeColumn(String line, int index)
  8. splitUsingIndexOf(String splittee, String splitter)
  9. sub(String string, int fromIndex, int toIndex)