Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 872 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get substring between two strings in DART?

#1
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]

Reply

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

#3
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));
}
Reply

#4
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*.
Reply

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

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

Reply

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through