Insert string at specified position of a string - Android java.lang

Android examples for java.lang:String Insert

Description

Insert string at specified position of a string

Demo Code

import java.io.UnsupportedEncodingException;
import java.util.*;

public class Main{

    /**/*from w w w.  ja  v  a  2  s  . c  o  m*/
     * Insert string at specified position of <code>src</code>
     *
     * @param src      string to be inserted
     * @param strInput insert content
     * @param position index
     * @return new string with insert content
     */
    public static String insertStringWithPosition(String src,
            String strInput, int position) {
        String result = src;
        if (src.length() > position) {
            String firstSection = subString(src, position);
            String secondSection = subString(src, position, src.length());
            result = firstSection + strInput + secondSection;
        }
        return result;
    }

}

Related Tutorials