By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

Fastest: Substring or IndexOf

P: n/a
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)
Jan 14 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Sun, 13 Jan 2008 19:52:30 -0800, Chizl <Ch***@NoShitMail.comwrote:
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
Jan 14 '08 #2

P: n/a
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>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
Doesn't IndexOf stop after the first one is found?
Jan 14 '08 #3

P: n/a
Doesn't IndexOf stop after the first one is found?

Yes it does. With that in mind it would be even faster (although probably
immeasurable) to use String.StartsWith() so that it doesn't bother looking
past the first char if it's not a match.

"Chizl" wrote:
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
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

Doesn't IndexOf stop after the first one is found?
Jan 14 '08 #4

P: n/a
On Sun, 13 Jan 2008 20:42:42 -0800, Chizl <Ch***@NoShitMail.comwrote:
Doesn't IndexOf stop after the first one is found?
Yes. But where would it stop if none is found? Why do you think that I
wrote in my previous post that on average, Substring() would be faster if
the search string is usually not part of the string?
Jan 14 '08 #5

P: n/a
Chizl wrote:
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)
IndexOf would inspect the entire string if your target is not found, so you
would not want that if you know ahead of time which part of the string you need
to look at. I would think that
if (szText.StartsWith("datagoeshere"))
would be your best bet if you know you are looking only at the first few bytes.

HTH,
-rick-
Jan 14 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.