0

1

 var userQuestion = '10+5-√x+96';

2

 var userQuestion = '10+5-√x+96-√x-2+√x';

i am expecting to get the value between square root and the next operator in the string and grouping them into different strings

how can i slice the string √x if i don't know it's index in dart.

and make other strings with respect to the number of √x in the string

eg in 1 i want a string slice that will create

var squareRoot = '√x';

and in 2 i want to get something like this

var squareRoot = '√x';
var squareRoot1 = '√x';
var squareRoot2 = '√x';
1
  • you want to extract all √x from string? Commented Dec 7, 2022 at 12:06

1 Answer 1

1

If you want all the squareroots including whatever comes after, until you hit an operator, then this should do the trick:

var userQuestion = '10+5-√x+96-√y-2+√5/√123*√256';
var regexp = RegExp(r'(√[\w\d]+)[+\-*/]?');
var matches = regexp.allMatches(userQuestion);
var squareRoots = matches.map((e) => e.group(1)).toList();
print(squareRoots);

Will print:

[√x, √y, √5, √123, √256]

There is obviously no way to declare variables with names as you did if it is an unknown number of square roots, so you'll have to use a List<String> that hold your strings, as I did above.

Sign up to request clarification or add additional context in comments.

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.