-1

Trying to split a String in certain ways without changing the String(String embed, String payload) structure.

System.out.println(embedCenter("<<>>", "Yay")); // => <<Yay>>

This is how it should look, so putting "<<>>" for embed and "Yay" for payload should return <<Yay>>, however for "()" embed and "Yay" Payload it should return "(Yay)" and for ":-)" embed and "Yay" payload it should return ":Yay-)"

So I'm just starting to learn, and kinda stuck at this question - I've tried doing substrings but while I could get one of those results, I cant find a way to get all of them with the same method.

public static String embedCenter(String embed, String payload) {
    return ""; // 
}

public static void main(String[] args) {
    System.out.println(embedCenter("<<>>", "Yay")); // => <<Yay>>
    System.out.println(embedCenter("()", "Yay")); // => (Yay)
    System.out.println(embedCenter(":-)", "Example")); // :Example-)
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • 1
    Divide the problem into smaller blocks first, then start solving those step-by-step. – Kayaman Oct 08 '19 at 06:48
  • "I've tried doing substrings" - where? Please post your attempt. – daniu Oct 08 '19 at 06:58
  • String subembed = embed.substring(0,2); String finembed = embed.substring(2,4); return subembed + payload + finembed; This basically does it for "<<>>" "Yay", If i put (0,1) and (1,2) it solves it for "()" If i put (0,1) and (1,3) it solves it for ":-)", however I'm not sure if theres a command for splitting the embed String before a new symbol appears (which would solve my problem) or after one kind of symbol ends. Also tried to put it into if else relation with boolean b = embed.contains("<<"), however it couldnt compile. – user12177474 Oct 08 '19 at 07:33
  • Hey @user12177474, welcome to StackOverflow :) You've done a good job trying to explain your problem, but we are still missing the exact problem statement: do you need to embed the second string into _the middle_ of the first string? In this case, what do you do when the number of symbols in the first string is not even? I mean, you should first describe the algorithm with words _thoroughly_, only then you will be able to work on a programmatic solution. – Scadge Oct 08 '19 at 08:02

1 Answers1

0

Ok, I did it, I thought way too complicated, did it really easy simply by dividing the String with length()/2, perfectly worked. Thanks for the input!

    int length = embed.length();
    String subembed = embed.substring(0,embed.length()/2);
    String finembed = embed.substring(embed.length()/2);
    return subembed + payload + finembed;