• 0

c# - string search - indexof too slow


Question

10 answers to this question

Recommended Posts

  • 0
  borked technique said:

Is there a fast way to search for a string in c#? or at least be able to use ststr from c? I have to go through a humongo loop and it takes forever with indexof.

Is your string made up of multiple words? Does it change much?

You could split the words out into an array, do a sort on it, and then use the BinarySearch feature of the Array object to find your word. It really depends on the string you are searching. If it doesn't change much, then cache the split and sorted string array for searching. If the string changes a lot, then it might be faster to use this approach over the loop/indexOf, but I've never done a timed comparison.

  • 0

Well I don't even want to imagine what kind of performance a 600 mb string object would have. Split it up if you can (Y)

But since you say you've created something to do it in 15 seconds, you've caught my attention; what did you do?

  • 0

I use a streamreader and streamwriter to read a line of text and write a line of text. I do a ToCharArray() on the string i am searching for and searching in before calling my search. Code below:

public void searchString(char[] sfor, char[] sin)

{

int i = 0;

int j = 0;

found = 0;

while (j < sin.Length - sfor.Length + 1)

{

if (i == sfor.Length)

{

found = 1;

break;

}

if (sfor == sin)

{

i++;

}

else

{

j++;

i = 0;

}

}

}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.