How to insert one string into the middle of another string? [duplicate]

You have to concat two substrings of the first string onto the ends of the second.

// put the marble in the bag
public static String insert(String bag, String marble, int index) {
    String bagBegin = bag.substring(0,index);
    String bagEnd = bag.substring(index);
    return bagBegin + marble + bagEnd;

Browse More Popular Posts

Leave a Comment