-19

I am trying to stick one string into the middle of another string, ex:

String One = "MonkeyPony";
String Two = "Monkey";

How would I put String Two into String One so it would read something like MonkeMonkeyyPony?

What I'm trying to do is insert "Monkey" into the middle of "MonkeyPony" numerous times, so on the first time it would read "MonkeMonkeyyPony", and on the second time it would read "MonkeMonMonkeykeyyPony", etc.

TylerH
  • 20,799
  • 66
  • 75
  • 101
user2150807
  • 411
  • 2
  • 7
  • 14
  • 7
    @TylerH By that time this question's asked, rules isn't that strict in SO, it isn't fair to the poster to flag this as duplicate after 6 years. – User2012384 Jul 16 '19 at 00:02
  • 9
    @User2012384 It's not about being "fair"; the purpose of SO is to provide a set of questions and answers that are useful to people who come across them in the future. Cleanup of old non-useful questions is part of that, and completely valid. Questions don't get a pass just because they're old. – Herohtar Jul 16 '19 at 05:42
  • @Herohtar This question did provide userful answer (Pointing a direction that using substring and loop is possible to achieve) – User2012384 Jul 16 '19 at 06:01
  • 8
    @User2012384 Marking a question as a duplicate has no effect on existing answers though. – Herohtar Jul 16 '19 at 06:05
  • 10
    @User2012384 You're incorrect. The rules on duplicates were the same when this question was asked as they are now. This question was a duplicate on the day it was asked; if you click through, you'll see the target question was asked two years prior. – TylerH Jul 16 '19 at 13:07
  • 2
    [related meta discussion](https://meta.stackoverflow.com/q/387205/839601) – gnat Jul 17 '19 at 09:44

3 Answers3

41

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;
}
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • would there be a way to loops this as well so instead of just inserting it into the middle it would be MonkeMonMonMonMonkeykeykeykeyyPony ? – user2150807 Mar 15 '13 at 22:32
  • 1
    It'd be less efficient, but you could chain this method a few times. – corsiKa Mar 16 '13 at 23:37
31

You can use StringBuilder.insert​(int offset, String str) to achieve this.

StringBuilder builder = new StringBuilder("MonkeyPony");
for(/* As often as necessary */) {
    int halfway = (int) (builder.length() / 2);
    builder.insert(halfway, "Monkey");
} return builder.toString();
MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
BakaTono
  • 479
  • 6
  • 13
-4

You don't need to use StringBuilder or any other complex method to do this. Here is the simplest way to achieve this. In this method I have just used the simple String methods.

import java.util.Scanner;

class Insert
{
    public static void main(String[] args)
    {
        System.out.println("Enter First String");
        Scanner scan = new Scanner (System.in);
        String str = scan.next();
        System.out.println("Enter Second String");
        Scanner scan2 = new Scanner (System.in);
        String str1 = scan2.next();
        int i = str.length();
        int j = i/2;

        if (i % 2 == 0)                    //Condition For Even
        {
            System.out.println(str.substring(0,j) + str1 + str.substring(j-1,(str.length() - 1)));
        }
        else 
        {
            System.out.println(str.substring(0,j) + str1 + str.substring(j,(str.length() - 0)));
        }
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 9
    This is the simplest method? The other answers from years prior are 1 and 5 lines, respectively. This one is 25 lines. – TylerH Jul 09 '19 at 20:51
  • 5
    Because they have debug statements, and it is simple in the sense of it's easy to understand what it's doing line by line. Shorter code != simplest method always. – Kaito Jul 10 '19 at 15:24
  • 5
    @TylerH without commenting on if this one is simpler, the other answers would obviously be more likely to be shorter, since they aren't complete program files; one is a method definition, while the other is a statement. They don't include the boilerplate code (or the debug statements, as Kaito pointed out) that this answer does – Honinbo Shusaku Jul 10 '19 at 15:49
  • @Abdul Yeah but he has made assumptions about what the code surrounding the two strings look like which in my opinion constitutes to noise. Why is there a scanner involved when the two strings are already known constants? – Avin Kavish Jul 16 '19 at 13:14
  • 1
    @UniqIdentifierAssignedAtBirth Are the 2 strings already known constants? I read the `ex` in `I am trying to stick one string into the middle of another string, ex:` as standing for "example", so I interpreted it as those 2 strings just being examples – Honinbo Shusaku Jul 16 '19 at 15:08