I have a string, myString, that is about 10000 in length.
If I do myString.Replace("A","B");
It will replace all instances of A to B.
How can I do that not to the entire string but only to character 5000-5500?
StringBuilder myStringBuilder = new StringBuilder(myString);
myStringBuilder.Replace("A", "B", 5000, 500);
myString = myStringBuilder.ToString();
It will require less memory allocations then methods using String.Substring().
var sub1 = myString.SubString(0,4999);
var sub2 = myString.SubString(5000,500);
var sub3 = myString.SubString(5501,myString.Length-5501);
var result = sub1 + sub2.Replace("A","B") + sub3;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With