• 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

    • Not what I was expecting. I thought it was going to add ray tracing to games that don't support it or something lol.
    • Looks less buggy than FC25. I still don't understand how they managed to release that game.
    • Yes, Command Palette, that's what I meant, thank you.
    • PowerToys Run has already been replaced with Command Palette. Run is still functional, but it's just a matter of time before Microsoft disables it. Raycast can do much more though, and with its plugins much, much, much more...
    • Snagit 2025.2.1 by Razvan Serea Snagit is the most complete screen capture utility available. Showing someone exactly what you see on your screen is sometimes the quickest and clearest way to communicate. With Snagit, you can select anything on your screen – an area, image, article, Web page, or error message – and capture it. Then, save the screen capture to a file, send it to Snagit​'s editor to add professional effects, share it by e-mail, or drop it into PowerPoint®, Word®, or another favorite application. Capture and share images, text or video from your PC. Create beautiful presentations, flawless documentation and quickly save online content. The latest version of Snagit offers a totally new interface and workflow - making SnagIt easier for beginners to use, while still providing maximum convenience and flexibility for the screen capture experts. Snagit 2025.2.1 fixes: Fixed an issue where tags applied to images might not save correctly. Fixed a crash that might occur when closing the Editor. Download: Snagit 64-bit | 419.0 MB (Shareware) Links: SnagIt Home Page | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Conversation Starter
      Kavin25 earned a badge
      Conversation Starter
    • One Month Later
      Leonard grant earned a badge
      One Month Later
    • Week One Done
      pcdoctorsnet earned a badge
      Week One Done
    • Rising Star
      Phillip0web went up a rank
      Rising Star
    • One Month Later
      Epaminombas earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      529
    2. 2
      ATLien_0
      205
    3. 3
      +FloatingFatMan
      168
    4. 4
      Michael Scrip
      150
    5. 5
      snowy owl
      126
  • Tell a friend

    Love Neowin? Tell a friend!