I really need some help removing some duplicated code. I've linked the methods causing me issues. http://pastebin.com/cZJihM4JAs you can see each method does similar things but retrieve different values. i.e one method to insert a string at a certain line. Another to get the the lines index and another to get the contents at a particular lineThere are things like:String text = textArea.getText();int start = 0;int count = 0;String buildNewTextArea = ;I could make these values global but not sure if this is the best way to approach this.My brain can't find away around this :(Thanks in advance public String insertCodeBlock(String newBlock) {    String text = textArea.getText();    int start = 0;    int count = 0;    String buildNewTextArea = ;    while (start >= 0) {        int nextLineStart = text.indexOf(NEW_LINE, start + NEW_LINE.length());        if (nextLineStart == -1) {            nextLineStart = text.length();        }        String[] temp = diffBlocks.get(indexOfLine).split(,);        int startLine = Integer.valueOf(temp[0]);        int endLine = (Integer.valueOf(temp[1]) - 1) + startLine;        if (count == startLine) {            buildNewTextArea += newBlock;        } else if (count < startLine || count > (startLine + (endLine - startLine))) {            buildNewTextArea += text.substring(start, nextLineStart);        }        count++;        start = text.indexOf(NEW_LINE, nextLineStart);    }    return buildNewTextArea;}private int getLineIndex(int line) {    String text = textArea.getText();    int start = 0;    int count = 0;    String buildNewTextArea = ;    int index = 0;    while (start >= 0) {        int nextLineStart = text.indexOf(NEW_LINE, start + NEW_LINE.length());        if (nextLineStart == -1) {            nextLineStart = text.length();        }        if (count == line) {            return index;        }        buildNewTextArea = text.substring(start, nextLineStart);        index += buildNewTextArea.length();        count++;        start = text.indexOf(NEW_LINE, nextLineStart);    }    return text.length();}public String getLineContent(int startLine, int endLine) {    String text = textArea.getText();    int start = 0;    int count = 0;    String buildNewTextArea = ;    while (start >= 0) {        int nextLineStart = text.indexOf(NEW_LINE, start + NEW_LINE.length());        if (nextLineStart == -1) {            nextLineStart = text.length();        }        if (count >= startLine && count <= endLine) {            buildNewTextArea += text.substring(start, nextLineStart);        }        count++;        start = text.indexOf(NEW_LINE, nextLineStart);    }    return buildNewTextArea;}
Struggling to refactor code to remove duplication
java
Maybe this can help?private static int getLineLength(String str, int line){        return str.split(\n)[line].length(); //line start 0    }    private static String getLineContent(String str, int startLine, int endLine){        String s = ;for(int i=startLine;i<=endLine;i++){s +=str.split(\n)[i] ;}return s;    }    private static String insertBlock(String sourceStr, String insertStr, int startLine, int endLine){    return getLineContent(sourceStr, 0, startLine).concat(insertStr).concat(getLineContent(sourceStr, endLine, sourceStr.split(\n).length-1));        }