26

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?

skip
  • 12,193
  • 32
  • 113
  • 153
  • It can be done but whats the exact format??? i mean is dance gonna be there always ?? – dku.rajkumar Jan 15 '12 at 17:26
  • @dku.rajkumar: Yep, the "dance" substring is always going to be there. `split()` should do the trick. I should have imagined a method like this already. I thought I might have to write a regex for it. Thanks :) – skip Jan 15 '12 at 17:33
  • then there is no need of regex, simply make use of String[] str = ;string.split("dance"); as mentioned answers – dku.rajkumar Jan 15 '12 at 17:36

6 Answers6

52

You can use String.split(String regex). Just do something like this:

String s = "123dance456";
String[] split = s.split("dance");
String firstSubString = split[0];
String secondSubString = split[1];

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.

Alex D
  • 29,755
  • 7
  • 80
  • 126
22

You can do this:

String str = "123dance456";
String substr = "dance";
String before = str.substring(0, str.indexOf(substr));
String after = str.substring(str.indexOf(substr) + substr.length());

Or

String str = "123dance456";
String substr = "dance";
String[] parts = str.split(substr);
String before = parts[0];
String after = parts[1];

It is noteworthy that the second answer not work if we have more than one occurrence of the substring. To that end, if we only want the first one to be recognized, it would be safer to call split with a limit:

String[] parts = str.split(substr, 2);

which ensures that the returned array has at most two elements. Also, since split will interpret its input as a regular expression we have to be wary of invalid regular expression syntax. As such, I would much rather the first solution, since it works irrespective of the composition of the original substring.

To make the first answer more efficient -- as it is my preferred answer -- then, we would need to remember the position of the substring:

final int position = str.indexOf(substr);
if (position >= 0) {
    //if the substring does occur within the string, set the values accordingly
    before = str.substring(0, position);
    after = str.substring(position + substr.length());
} else {
    //otherwise, default to the empty string (or some other value)
    before = "";
    after = "";
}

It always pays to pay attention to these little edge cases.

rsp
  • 23,135
  • 6
  • 55
  • 69
Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
5

If you are using commons-lang see StringUtils.substringAfter()

Constantine Gladky
  • 1,245
  • 6
  • 27
  • 45
4

Easiest is to use the split method as the other answers suggest. You can also use a Matcher and a Pattern for a more general approach:

String str = "123dance456";
String splitter = "dance";
Pattern p = Pattern.compile("(.*?)" + splitter + "(.*)");
Matcher m = p.matcher(str);
if (m.matches()) {
    firstSubString = m.group(1); // may be empty
    secondSubString = m.group(2); // may be empty
} else {
    // splitter not found in str
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1
String[] data = new String("123dance456").split("dance");
kcdragon
  • 1,724
  • 1
  • 14
  • 23
0

Using the Scanner is a nice alternative:

Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
if ( scanner.hasNext() )
  System.out.println( scanner.next() );
if ( scanner.hasNext() )
  System.out.println( scanner.next() );   

It is quite convenient to process the tokens as a stream and simply ask via hasNext() if new tokens are available. Also you get nice conversions from the Scanner, even localised, like:

Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
System.out.println( scanner.nextDouble() * scanner.nextDouble() );   
Christian Ullenboom
  • 1,388
  • 3
  • 24
  • 20