Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, July 24, 2016

how to find before and after sub-string in a string

how to find before and after sub-string in a string


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?

Answer by Milad Naseri for how to find before and after sub-string in a string


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.

Answer by Alex D for how to find before and after sub-string in a string


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.

Answer by kcdragon for how to find before and after sub-string in a string


String[] data = new String("123dance456").split("dance");  

Answer by Ted Hopp for how to find before and after sub-string in a string


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  }  

Answer by Constantine Gladky for how to find before and after sub-string in a string


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.