Question

I want to write an if statement that would essentially say, "If what was entered into the last Console.ReadLine() method contained an "!""...do something.

So, I've got a method:

static void Operations(StringBuilder bleh, int ops, int count, string str)
{
    for (int i = 0; i < ops; i++)
    {
        bleh.Append(Console.ReadLine()); // Potential value of "ops" is > 100,000

        if (bleh.ToString().IndexOf("!") != -1)
        {
            bleh.Replace("!", "");
        }
        else
        {
            bleh.Remove(0, 1);
            bleh.Replace("^", "");
        }
        ...

On every readLine() there is either a "!" + a letter entered or just a "^". If a "^" is entered the stringbuilder's first index char is removed as well as the "^". If a "!" is entered, just the "!" is removed, but the letter remains.

My code seems slow because after I've appended a new string from the ReadLine(), it has to search for a "!" or a "^" in a potentially huge string on every iteration of the loop, and then search again to remove it. It would be much better if I could keep track of when the "^" and "!" were entered thereby allowing me to replace them by using the index values:

bleh.Replace(counter, 1)

Getting the index values here are a little tricky to get at since the size of the stringbuilder is growing and shrinking depending on the entered values.

This brings up an interesting question: How is something like, "StringBuilder.Replace("abc", "a") done? For a string that's say 10,000 characters long, is it searching from the beginning of the string and "scrolling" through it to find "abc"? Since the .Append method is putting the "!2", for example, at the end, the the performance is compromised.

Is there an easy answer that would increase performance?

Was it helpful?

Solution

It seems like ! and ^ are being replaced as they are being inputted. That is, beyond the current input line, those characters cannot appear at all in StringBuilder bleh.

If that is the case, then you should only need to search/replace in the current input string.

meh = Console.ReadLine();

        if (meh.ToString().IndexOf("!") != -1)
            meh.Replace("!", "");

        else
        {
            bleh.Remove(0, 1);
            meh.Replace("^", "");
        }
bleh.Append(meh);

If you still need to know whether an input line had a particular character (or similar) after doing the replace, I would recommend storing those lines in a List of Strings rather than all together in a StringBuilder. This way, you can store different input types into different lists or use objects to hold the string and some metadata.

If this is not sufficient, then you might have to resort to keeping an index of the locations of special characters. Search the input string, get indices of relevant characters (plus the StringBuilder's length), then append the input string. Store those indices in a list for later lookup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top