Nikhil Lohar
Nikhil Lohar

Reputation: 127

Split String using flutter

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

Answers (2)

HBS
HBS

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

C.P.O
C.P.O

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

Related Questions