36 thumbs up

String.IndexOf search algorithm

Does the string.IndexOf method in the .Net framework use the Boyer-Moore search algorithm?


  • 1409 views
Share Send to a friend Watch Report
 

Best Answer

 
518 thumbs up

They're coming to take me away, Ha-haaa!

Advanced .NET Debugging Blog

My personal blog

The MSDN documentation states in the documentation for String.IndexOf "This method performs a word (case-sensitive and culture-sensitive) search using the current culture. The search begins at the first character position of this instance and continues until the last character position." which imply that that the algorithm is not linear and since Boyer-Moore is an algorithm that is suppose to run in less than linear time, we can assume String.IndexOf doesn't use it.

On the otherhand, the regular expressions (RegEx) implementation in .NET DOES use the Boyer-Moore algorithm (which is O(n/m)) so for longer strings it would be faster.

I've found this discussion about this subject in a message board which talks about when to use RegEx and touches this point of String.IndexOf implementation.

There is also an article in codeproject.com that has an implementation of the Boyer-Moore algorithm.


Posted 2 years ago ( permalink )
In reply to gvina's question
Eran was invited by Yedda to answer this question.

Rated as
Best Answer
0
5

Helpful?

line
line
line


 

All Answers

Order by
 
340 thumbs up

Rejected slogan #235:

Yedda - a brain the size of a planet.

This is not a full answer, but:

String.IndexOf(String) calls CultureInfo.CurrentCulture.CompareInfo.IndexOf, which calls (at least in some of the cases) CompareInfo.SyntheticIndexOf, which, in decompiled pseudo-code, looks like this:

private int SyntheticIndexOf(string source, string value, int start, int length, int nativeCompareFlags)

{

      if (CompareInfo.fFindNLSStringSupported >= 0)

      {

            int num1 = CompareInfo.FindNLSStringWrap(this.m_sortingLCID, nativeCompareFlags | 0x400000, source, start, length, value, value.Length);

            if (num1 >= -1)

            {

                  return num1;

            }

      }

      for (int num2 = 0; num2


Posted 2 years ago ( permalink )
In reply to gvina's question
Rated as
#3 out of 4
0
0

Helpful?

line
line
line



 
340 thumbs up

Rejected slogan #235:

Yedda - a brain the size of a planet.

Hmpff. Looks like my answer was truncated. Sorry about that!

Anyway, the point was that looking at the decompliation results, it's clear that at least in some cases the implementation is strictly linear.

However, if it was up to me, I'd actually vote for Eran's answer :)

In fact, I might just do that :)


Posted 2 years ago ( permalink )
In reply to Yaniv's answer
Rated as
#2 out of 4
0
1

Helpful?

line
line
line



 

You can just download the Reflector tool from http://www.aisto.com/roeder/dotnet/Download.aspx?File=Reflector and check it out yourself by looking at the String.IndexOf implementation.

 


Posted 2 years ago ( permalink )
In reply to Yaniv's answer
Rated as
#4 out of 4
0
0

Helpful?

line
line
line



Sign in to participate

Got an answer for gvina? Would you like to comment on the posted answers, or vote for the one which you think is the best?

Sign up for a free account, or sign in (if you're already a member).

Explore Related Questions

Other people asked questions on similar topics, check out the answers they received:


What is c#

what is c#
Submitted by pradeepbaloni 5 months ago
  • viewed 262 times

Last answer posted 4 months ago by Eran


Get a C# string from text box

How do I get a C# string from a text box? If I have a text box with new lines and tabs I want to get the real C# string for it ...
Submitted by dudushmaya 2 years ago
  • viewed 2189 times

Last answer posted 7 months ago by tprabu


What data type do the range validator?. give some ...

what data type do the range validator?. give some example code.
Submitted by ravi_pts1 1 year ago
  • viewed 592 times

Last answer posted 1 year ago by SethGummiebear



» More...

Feed - Subscribe to changes to this Q&A Blog