0

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.

3

3 Answers 3

3

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

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

Comments

3

You can do:

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

4 Comments

Probably he also meant to have all permutations of "zero or one blanks"?
I just want the string character excluding white spaces in between .
@MichaelPerrenoud: var lineValues = new string[] { " Order 1 QTY ", " Order 1QTY" }; returns -1 here, while I expected 2.
@PatrickHofman, yes if the strings were that disparate you'd get some weird results.
0

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

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

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.