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

String.StartsWith("....")

P: n/a
Hi,

I am using lots of string comparisons using
String.StartsWith(subString) method where subString might be more than
just couple of chars (e.g. "This is the string to compare ")

I have noticed that it executes quite slowly. Is there a way to do
this faster?

tx
_dino_
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Dino Buljubasic" <di**@noplacelikehome.com> wrote in message news:qe********************************@4ax.com...
I am using lots of string comparisons using
String.StartsWith(subString) method : : Is there a way to do this faster?


You can add all of the subStrings you want to compare against into a SortedList.
Then take the string you're testing and go one-character at a time.

When it starts with 'T', that immediately eliminates most of the subStrings you are
comparing against, narrowing you down to just those strings in the SortedList that
begin with 'T'. When the second character is 'o', that eliminates every string that
starts out like "The" or "Those" or "Tactfully."

Inevitably, you are only checking each character in your string no more than one
time, and you whittle down the number of subStrings you're comparing against
very quickly so there isn't that many comparisons going on.
Derek Harmon
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.