37

How can i achieve similar solution to: How to get a substring between two strings in PHP? but in DART

For example I have a String:
String data = "the quick brown fox jumps over the lazy dog"
I have two other Strings: quick and over
I want the data inside these two Strings and expecting result:
brown fox jumps

7 Answers 7

79

You can use String.indexOf combined with String.substring:

void main() {
  const str = "the quick brown fox jumps over the lazy dog";
  const start = "quick";
  const end = "over";

  final startIndex = str.indexOf(start);
  final endIndex = str.indexOf(end, startIndex + start.length);

  print(str.substring(startIndex + start.length, endIndex)); // brown fox jumps
}

Note also that the startIndex is inclusive, while the endIndex is exclusive.

Sign up to request clarification or add additional context in comments.

6 Comments

I was almost about to post an answer with RegExp. But this one's better
My String is actually unformatted HTML and is very long. substring method failed with RangeError. So, @Sergey Solodukhin's answer worked for me. This one is awesome for smaller Strings. Thank you.
@SukhchainSingh it was a bug. I've just fixed it
@RémiRousselet Thank you, it worked. It's lot faster than regexp as expected :)
What's about real data? Word order may change "the over quick brown fox jumps the lazy dog" or one keyword may be absent.
|
10

I love regexp with lookbehind (?<...) and lookahead (?=...):

void main() {
  var re = RegExp(r'(?<=quick)(.*)(?=over)');
  String data = "the quick brown fox jumps over the lazy dog";
  var match = re.firstMatch(data);
  if (match != null) print(match.group(0));
}

1 Comment

Thank you, my string was very large so Rémi Rousselet's answer didn't helped. This one worked like a charm.
5
final str = 'the quick brown fox jumps over the lazy dog';
final start = 'quick';
final end = 'over';

final startIndex = str.indexOf(start);
final endIndex = str.indexOf(end);
final result = str.substring(startIndex + start.length, endIndex).trim();

Comments

3

For future researchers: To start form the beginning of a string to a specific symbol or character;

Using substring and indexOf which is faster than regExp do this:

void main() {
  const str = "Cars/Horses";

  final endIndex = str.indexOf("/", 0);

  print(str.substring(0, endIndex)); // Cars
}

This might just help someone

Comments

2

You can do that with the help of regex. Create a function that will return the regex matches as

Iterable<String> _allStringMatches(String text, RegExp regExp) => 
regExp.allMatches(text).map((m) => m.group(0));

And then define your regex as RegExp(r"[quick ]{1}.*[ over]{1}"))

Comments

1

The substring functionality isn't that great, and will throw errors for strings above the "end" value.

To make it simpler user this custom function that doesn't thrown an error, instead using the max length.

String substring(String original, {required int start, int? end}) {
  if (end == null) {
    return original.substring(start);
  }
  if (original.length < end) {
    return original.substring(start, original.length);
  }
  return original.substring(start, end);
}

Comments

1
void main() {
  String str = "the quick brown fox jumps over the lazy dog";
  String start = "quick";
  String end = "over";

  print(getSringBetween(str, start, end));
  //OUT PUTS: brown fox jumps
}

String getSringBetween(str, start, end) {
  return str.split(start).last.split(end)[0].trim();
}

2 Comments

I love this, short and easy. Just appended this to a string.
Instead of [0] you can just use .first it's already initialized in the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.