Lucas_Santos
Lucas_Santos

Reputation: 4740

How Can I do a multiple Split

How can I do a multiple Split ?

Example

string eq = SIN(X)/3

I can SPLIT this like string equation[] = eq.Split['/'] but if I have +, -, *, / in my equation how can I SPLIT this ?

Example

string eq = SIN(X) + 3 / 3 * 4

Upvotes: 2

Views: 114

Answers (2)

Yogendra Singh
Yogendra Singh

Reputation: 34367

Use Regex.Split and use the regex to match the operators e.g. below:

string[] equation = Regex.Split(eq, "[/\+\*-]");

Supply all possible operators in the regex expression.

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174389

string.Split allows to pass in multiple characters:

var result = eq.Split('/', '+', '-', '*');

Having said that, it is not a good idea to evaluate such expressions by using string operations. You should use a mathematical parser for this task.

Upvotes: 7

Related Questions