c# - Is String.Contains() faster than String.IndexOf()? -


I have a string buffer of approximately 2000 characters and need to check the buffer if there is a specific string in it.

Does anyone know what works better than that?

  / / S1 in 2000 characters, search token in s2 string s1 = "too many letters. Quick brown fox jumps over the celestial dog"; String S2 = "Fox"; Bull B; B = s1. Resources (S2); Int i; I = s1.IndexOf (S2);  

is included calls indexoff :

  is included in the public child (string value) {returns (this index (value, string compiler, ordinal)> gt; = 0); }  

Which call compareEFO.inxoff , which ultimately uses CLR implementation.

If you want to see how strings are compared CLR (Search for CaseInsensitiveCompHelper )

Indexoff (string) does not have any option and is included () A normal comparison (instead of trying to use a byte by byte comparison, instead of smart comparison, for example, e with e) .

Then IndexOf will be moderate (in principle) as indexOf goes directly to the string search Kernel32.dll (the power of the reflector!) By using FindNLSString.

Updated for .NET 4.0 - Indexoffs no longer use the Ordinary Comparison and may include fast. See the comment below.


Comments