Thursday, April 21, 2011

Workaround for String.Contains() in C# .NetCF 2.0?

There is a string method called Contains. It allows you to quickly search a string for another string. I need to use this in a .netcf 2.0 application but per MSDN it does not come available until the 3.5 framework.

Can anyone offer a work around (C#)?

TIA Noble

From stackoverflow
  • Hi, You could try using String.IndexOf. If it returns -1, the string does not exist inside the other string.

  • What about string.IndexOf and just check to see if it returns greater than -1?

  • Browsing "String.Contains" in Reflector gives below. I think this can be used directly in code.

    Public Function Contains(ByVal value As String) As Boolean
        Return (Me.IndexOf(value, StringComparison.Ordinal) >= 0)
    End Function
    
    

    Also a C# version

    public bool Contains(string value)
    {
        return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
    }
    

0 comments:

Post a Comment