0Day Forums
How to get substring between two strings in DART? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Flutter & Dart (https://0day.red/Forum-Flutter-Dart)
+--- Thread: How to get substring between two strings in DART? (/Thread-How-to-get-substring-between-two-strings-in-DART)



How to get substring between two strings in DART? - unitalicizedsnh - 07-21-2023

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

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

[1]:

[To see links please register here]




RE: How to get substring between two strings in DART? - antivenin90 - 07-21-2023

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}"))`


RE: How to get substring between two strings in DART? - stadias676537 - 07-21-2023

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));
}


RE: How to get substring between two strings in DART? - charylzntwaa - 07-21-2023

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


```dart
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*.


RE: How to get substring between two strings in DART? - interlinement250302 - 07-21-2023

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();


RE: How to get substring between two strings in DART? - trentonfefvtnjanw - 07-21-2023

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);
}




RE: How to get substring between two strings in DART? - elidakmvwl - 07-21-2023

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**