I have a string say 123dance456 which I need to split into two strings containing the first sub-string before the sub-string dance (i.e. 123) and after the sub-string dance (i.e. 456). I need to find them and hold them in separate string variables, say String firstSubString = 123; and String secondSubString = 456;.
Is there any given String method that does just that?
You can use
String.split(String regex). Just do something like this:Please note that if
"dance"occurs more than once in the original string,split()will split on each occurrence — that’s why the return value is an array.