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*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top