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.

Ian
  • 1,221
  • 1
  • 18
  • 30
  • 2
    Maybe with the help of [Regular Expressions](http://stackoverflow.com/questions/18701992/regex-space-or-no-space)? – Uwe Keim Oct 13 '14 at 11:56
  • possible duplicate of [C# string comparison ignoring spaces, carriage return or line breaks](http://stackoverflow.com/questions/4718965/c-sharp-string-comparison-ignoring-spaces-carriage-return-or-line-breaks) – Michal Hosala Oct 13 '14 at 11:58
  • Then You will have to buffer the String in another tmp variable, delete the blanks of it and then search again with a search pattern also without blanks. – icbytes Oct 13 '14 at 11:58

3 Answers3

3

You can do:

string y = "Order 1 QTY";
int Quantity = Array.FindIndex(lineValues, x => x.Equals(y) || x.Equals(y.Replace(" ","")));
fatihk
  • 7,789
  • 1
  • 26
  • 48
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

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
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"));
Ian
  • 1,221
  • 1
  • 18
  • 30