I have a line of one million letters A. In the loop, I replace the first occurrence of the letter A with "" until the letters end.
Before replacing, I must determine whether there is an occurrence in the string or not, and and I do it this way:
var inx = a.IndexOf(b, StringComparison.Ordinal);
if (inx > -1)
{
RemoveAndInsertString(ref a, ref b, ref c, inx);
}
I have tried:
private void RemoveAndInsertString(ref string sourceString, ref string rowToRemove, ref string rowToInsert, int inx)
{
Regex rgx = new Regex(rowToRemove);
sourceString = rgx.Replace(sourceString, rowToInsert, 1);
}
and
private void RemoveAndInsertString(ref string sourceString, ref string rowToRemove, ref string rowToInsert, int inx)
{
if (!String.IsNullOrEmpty(rowToInsert))
{
StringBuilder sb = new StringBuilder(sourceString.Length + rowToInsert.Length);
sb.Append(sourceString.Substring(0, inx));
sb.Append(rowToInsert);
sb.Append(sourceString.Substring(inx + rowToRemove.Length, sourceString.Length - (inx + rowToRemove.Length)));
sourceString = sb.ToString();
}
}
but it's so slow! I made a test, for a string length of 1,000,000: 0.5 seconds for 10,000 replacements and 52 seconds for 100,000 replacements. If the string contains 1,000,000 characters to be replaced - I can't wait for it!
How can I speed up the replacement?
I need make an remark: it's not a loop by ONE string. The situation looks like this: I have a 100000 rows. Rows can have up to 1000000 characters. I should make a replace of substring, if it's exist in the string, or skip this string and make some actions if no substring in the string. And, the lines used in the program do not support Unicode, if you need it. Example with A - it's a bad example, i agree, but it's a test, that testing performance of my program (it's not my test)