-1

So, I am trying to split a string but I don't just want to split it by single character strings, but rather by entire strings.

So if my String is ["hello buddy my name is buddy"], my delimiter would be "buddy".

I want it to split into: ["hello", "my name is"].

I hope i'm not over complicating it.

Jazib
  • 1,343
  • 13
  • 27

1 Answers1

1
public static void main(String[ ] args) {
    String test = "hello buddy my name is buddy";
    String[] res = test.split("buddy");
    for (String s: res) {
        System.out.println(s);
    }
}

Seems to do what you want, no?

yalpsid eman
  • 3,064
  • 6
  • 45
  • 71
  • 1
    Keep in mind that if you will be splitted based on user-supplied input, you'll want to pass it to `Pattern.quote()` first, e.g. `String[] res = test.split(Pattern.quote("buddy"));` – Sean Bright Oct 07 '19 at 21:13