• 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.
  • Posts

    • Mozilla should shut down everything except the browser and the services they whitelabel (for example Firefox VPN from Mullvad). Everything else just cost them money and have never returned any revenue. Focusing in the browser is what makes sense considering what is going to change regarding their income sources. About Firefox and chromium... if they ever have to abandon Gecko because of financial issues it is more likely to use Apple's WebKit and not Chromium and Blink. WebKit is more versatile for Firefox because it will allow them even to keep Firefox extension ecosystem. The same way GNOME Web which is a WebKit browser has experimental Firefox extension support.
    • I really dont like how they’ve put shadows under window elements making the sidebar and toolbar buttons look like they sit above the window itself. Looks very odd and clunky.
    • Rubbish. It's chock-full of ads and with manifest 3.0,the gloves are off. Google does not even pretend now. They want évil and want your data
    • Glary Utilities 6.27.0.31 by Razvan Serea Glary Utilities offers numerous powerful and easy-to-use system tools and utilities to fix, speed up, maintain and protect your PC. Glary Utilities allow you to clean common system junk files, as well as invalid registry entries and Internet traces. You can manage and delete browser add-ons, analyze disk space usage and find duplicate files. You can also view and manage installed shell extensions, encrypt your files from unauthorized access and use, split large files into smaller manageable files and then rejoin them. Furthermore, Glary Utilities includes the options to find, fix, or remove broken Windows shortcuts, manage the programs that start at Windows startup and uninstall software. All Glary Utilities tools can be accessed through an eye-pleasing and totally simplistic interface. Glary Utilities 6.27.0.31 changelog: Optimized Disk Cleaner: Optimized the scanning algorithm for Firefox to enhance user experience. Optimized Disk Cleaner: Added support for Nitro Pro 14. Optimized Tracks Eraser: Optimized the scanning algorithm for Firefox to enhance user experience. Optimized Tracks Eraser: Added support for Nitro Pro 14. Optimized Startup Manager: Optimized the review algorithm to enhance user experience. Optimized Uninstaller Manager: Optimized the scanning algorithm to enhance user experience. Minor GUI improvements. Minor bug fixes. Download: Glary Utilities 6.27.0.31 | 27.0 MB (Freeware) Download: Portable Glary Utilities | 32.3 MB View: Glary Utilities Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      CoolRaoul earned a badge
      One Month Later
    • First Post
      Kurotama earned a badge
      First Post
    • Collaborator
      Carltonbar earned a badge
      Collaborator
    • Explorer
      MusicLover2112 went up a rank
      Explorer
    • Dedicated
      MadMung0 earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      ATLien_0
      269
    3. 3
      +FloatingFatMan
      242
    4. 4
      +Edouard
      201
    5. 5
      snowy owl
      168
  • Tell a friend

    Love Neowin? Tell a friend!