-3

I have a list of string that contains few strings with some special characters like -, [, ], (, ).

I am passing a string to a method where I need to parse the above list & find if the exact match is found or not.

For this I have written the below code, but when ever the special characters are present in the string, its not able to compare.

foreach (var item in myList)
{
    if (myInput.Trim().ToUpper() == item.Trim().ToUpper() ) //Here myInput is "In - Com [SP]"
    {
        count++;
    }

}

But if I compare a static string by a preceding @ symbol, then the compare is working fine. For Example :

if (item == @"In - Com [SP]")
{
    count++;
}

Can anyone please help me how to incorporate this for a dynamic list of strings? NB : We cant concatenate @ with a string variable.

Any ways exist by using Regex?

Biki
  • 2,518
  • 8
  • 39
  • 53
  • 5
    please provide a bit more of a [mcve] – Daniel A. White May 17 '16 at 15:20
  • 2
    You need to tell us what's in `myInput` and `myList`. – Dour High Arch May 17 '16 at 15:21
  • 2
    String comparison should generally be done using `Equals()` method. [Check here for the differences](http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp) – Draken May 17 '16 at 15:22
  • 1
    The @" string opener instructs the string to ignore escaped characters such as \n which would be new line, it has no effect on the string compare, inwhich case the problem will probably be in your strings – MikeT May 17 '16 at 15:26
  • 1
    try adding `Console.WriteLine("\""+myInput.Trim().ToUpper()+"\" \"" +item.Trim().ToUpper()+"\"");` this will give you a better idea about what is being compared – MikeT May 17 '16 at 15:31
  • Thx, but I found the String.Equals() also doesn't work when special characters present in both strings to be compared. You may try comparing "In - Com [SP]" as both strings & see if its working. – Biki May 17 '16 at 16:14

3 Answers3

1

I could not tell you why your version is not working, unless there are some unusual line breaks or other spurious characters that are creating an issue when comparing strings. (This could happen for example if a unix line ending or other unusual character was somehow in the string that you could not see)

I would try using the static Compare method for String where the true parameter will compare the string, ignoring case:

foreach (var item in myList)
{
    if (String.Compare(myInput.Trim(), item.Trim(), true) == 0)
    {
        count++;
    }

}

Updated: After reading the comments, Draken has suggested using the string Equals method.

foreach (var item in myList)
{
    if (myInput.Trim().Equals(item.Trim(), StringComparison.OrdinalIgnoreCase))
    {
        count++;
    }

}
Jonathan Walton
  • 449
  • 3
  • 18
  • Nope, when ever the special characters are present in the strings (to be compared), its not able to compare with the above mentioned method. :( – Biki May 17 '16 at 16:06
  • Only this way( by appending @ sign), it works ... if (item == @"In - Com [SP]") { count++; } – Biki May 17 '16 at 16:07
  • @Biki, can you find anything when you use the debugger? Or another thing I would try is to paste each version of the string into Notepad ++ or similar. You can then go to menu View > Show Symbol > Show All Characters. At least when your issue happened once to me, I found that it was a difference between Unicode and ASCII characters. Also check out http://stackoverflow.com/a/15259355/5889360, since if there are characters you can do without, you might be able to use this article to help find a solution. – Jonathan Walton May 17 '16 at 21:42
1

For exact string equality you should use StringComparer.Ordinal. And, as I can see that you don't care about case, it's better for you to use StringComparer.OrdinalIgnoreCase :

if(StringComparer.OrdinalIgnoreCase.Equals(myInput.Trim(), item.Trim()))
{
    count++;
}
Verbon
  • 514
  • 4
  • 7
  • 1
    Nope, when ever the special characters are present in the strings (to be compared), its not able to compare with the above mentioned method. :( – Biki May 17 '16 at 16:06
1

If you want to count, just Count() with a help of Linq; to test if strings are equal use String.Equals:

System.Linq;
...
// do not do Trim() repeatedly
String testValue = myInput.Trim();

int count = myList
  .Count(item => String.Equals(item.Trim(), 
                               testValue,
                               StringComparison.OrdinalIgnoreCase));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • if you are going to shift them from plain c# to linq then you need to make sure they are aware that they need the Linq library included – MikeT May 17 '16 at 15:37
  • Thx, but the problem is not lying with different method of doing the same thing, but I found the String.Equals() doesnt work when special characters present in item or in testValue of ur example. You may try comparing "In - Com [SP]" as both string & see if its working. – Biki May 17 '16 at 16:11