Replacing the x-th character in a string?

Stellaferox

Active Member
Licensed User
Hi,

Was wondering how you could replace the x-th character in a string with another character.
Let's say I have a string "11111111" and I want to replace the x-th "1" with a zero, how do I do that?

Thanks and BTW I know of course of the existence of BitWise.dll and the bit-operators but need this in a string.......

Marc
 

agraham

Expert
Licensed User
Longtime User
Strings in .NET are not "mutable". That is you can't change a string, you can only make a new one.

For simple manipulation you would just use Substring to picks out the bits of the old string that you want and add them together. My http://www.b4x.com/forum/additional-libraries/2022-stringsex-library.html#post10957 makes it a bit easier.

NewString = Left(OldString, x) & NewChar & Right(OldString, StrLength(OldString -x -1)

I haven't checked this so there may be an "off by one" in the indexing but you get the idea. For more complex manipulations a StringBuilder, from StringsEx, is more efficient than stringing normal strings together.
 

Stellaferox

Active Member
Licensed User
Yes, thnx Agraham. I just feared that was the solution. In fact I already heave this implemented but as I hate concatenating with indexes and substrings I hoped there would be a "simple" solution like back in the old days like String[x] = "1".
Thanks again!
Marc
 
Top