If I know what the first 12 bytes of a string is, which would be faster
to
validate those first 12 bytes?
if(szText.Substring(0,12) == "datagoeshere")
or
if(szText.IndexOf("datagoeshere")==0)
Well, which one is faster when you write a simple test application and
test it yourself? Doing that is the only way anyone here would be able to
answer your question with certainty.
It's also not an apples-to-apples comparison. Again, haven't done the
test myself, so this is a guess, but...I suspect one will (on average) be
faster if you normally have a match (IndexOf), the other will be faster if
you usually don't have a match (Substring).
You can probably get the best of both worlds by using this:
if (szText.IndexOf("datagoeshere", 0, 12) == 0)
This allows you to avoid having to create a new string instance, but also
prevents IndexOf from bothering to look at the rest of the string if the
search string isn't found right at the beginning.
Pete