I am having a string String S1="https://www.microsoft.com/s/5f5wpyvu1cbdek2ld3as"

I am having another string String S2="0000000/demowidget".

I want to insert my new string S2 into my old string S1 just before "/s"

any help?

  • Good question, would it be suitable for Stack Overflow, you will get a larger audience perhaps.
    – Bartley
    Nov 20, 2013 at 9:23

1 Answer 1


Lets say you have the following strings.

String1 = 'myString/myPattern/someString'
String2 = 'newString'

You can append your new string just before the required pattern like this.

String newString = 
    String1.substring(0, String1.indexOf('/myPattern')) +
    String2 + 
    String1.substring(String1.indexOf('/myPattern'), String1.length());

But make sure your pattern is not repeated in the String1, because you might end up replacing just the first occurrence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.