0

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?

1
  • What datatype do you expect the operators to be in that list? The output you expect is impossible. Commented Jul 31, 2020 at 20:11

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

You may also want to show how to parse the numbers in the output to ints as that's what the OP's intended output implies.
0

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

Comments

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.