Replacing the x-th character in a string?

Discussion in 'Questions (Windows Mobile)' started by Stellaferox, Aug 20, 2009.

  1. Stellaferox

    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
     
  2. agraham

    agraham Expert Licensed 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.basic4ppc.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.
     
  3. Stellaferox

    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice