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.