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?