StringBuilder .Replace("a", "aa") = OutOfMemoryException

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

Hello, if I try this

((StringBuilder)sb).Replace("a", "aa");

I get OutOfMemoryException raised if sb contains at least one "a"...

It seems like StringBuilder 'seeker' doesn't move to the end of
replacement string but it moves to the second character of the
replacement string...

((String)s).Replace("a", "aa");

seems ok...

What would be the solution?

TIA... AD
 
Hi AD,

You could always do it the hard way

int i = 0;
while((i = s.IndexOf("a", ++i)) > -1)
{
s = s.Insert(++i, "a");
}
 
Ad,

Are you sure,

This test gave for me "aaaaaa"
\\\
System.Text.StringBuilder sb =
new System.Text.StringBuilder("aaa");
((System.Text.StringBuilder) sb).Replace("a","aa");
///

I hope this helps?

Cor
 
Morten, thank you very much :))

Cor, you're right! I was measuring the time spent of quite a few
iterations... :))

AD *BLUSH*
 
Back
Top