Reputation: 127
So I'm trying to split a string which contains operator.
Here is the example :
var string= "12+13+45-78*45/91=100"
(how can i achieve this using dart)
var result = [12,+,13,+,45,-,78,*,45,/,91,=,100]
Does anyone have a solution?
Upvotes: 0
Views: 703
Reputation: 670
You can use RegExp for this:
void main() {
final regex = RegExp(r"(?<=\+|-|\*|/|=)|(?=\+|-|\*|/|=)");
final str = "12+13+45-78*45/91=100";
final splitList = str.split(regex);
}
Upvotes: 0
Reputation: 1281
You can do it with something like:
RegExp exp = new RegExp(r"\d+|\+|-|\*|/|=");
String str = "12+13+45-78*45/91=100";
Iterable<Match> matches = exp.allMatches(str);
var list = matches.map((m) => (m.group(0)));
print(list);
it will print:
(12, +, 13, +, 45, -, 78, *, 45, /, 91, =, 100)
Upvotes: 2