user3913587
user3913587

Reputation: 41

Find a particular string in an string array

I am trying like this:

int Quantity = Array.FindIndex(lineValues, x => x.Equals("Order 1 QTY"));

It is passing for the same string. But I want it to get passed even if there are no spaces between the string.

I want it to get passed with both the string:

"Order 1 QTY"
"Order1QTY"

I want to check for just string excluding spaces.

Upvotes: 0

Views: 109

Answers (3)

Ian
Ian

Reputation: 1251

Alternatively, remove all the whitespace from your test string, then compare that to "Order1Qty".

int Quantity = Array.FindIndex(lineValues, 
    x => x.Replace(" ", "").Equals("Order1QTY"));

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67918

One approach would be to use a regular expression:

var regex = string.Format("Order\s*{0}\s*QTY", 1);
int Quantity = Array.FindIndex(lineValues, x => Regex.Matches(x, regex));

The regular expression I'd use would be something like this:

Order\s*1\s*QTY

Regular expression visualization

Debuggex Demo

Upvotes: 3

fatihk
fatihk

Reputation: 7929

You can do:

string y = "Order 1 QTY";
int Quantity = Array.FindIndex(lineValues, x => x.Equals(y) || x.Equals(y.Replace(" ","")));

Upvotes: 3

Related Questions