CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2000
    Location
    Athens, Greece
    Posts
    137

    howto overwrite a char in a string?!

    hi all,

    i came across this very basic need, i want to replace a character in a string with another character.

    Dim s as string
    s = "this iz a test"
    s(7) = "s" 'this would be nice, but gives an error




    i cant find anything -not even a hint- about some existing function or operator which gives me this functionality. I tried everything i could think of, nothing worked.

    I'm kinda reluctant to start writing a new function which takes apart the string, removes the unwanted character, puts the new character in its place, and glues everything back together again.

    Am i just being blind, not able to find anything existing for this problem, or am i just thinking too difficult so that i overlook a simple solution right in front of me?

    Anybody?

    thanx


  2. #2
    Join Date
    Feb 2000
    Location
    Ireland
    Posts
    808

    Re: howto overwrite a char in a string?!

    Use the Replace function in VB6

    Dim s as string
    s = "this iz a test"
    s=replace(s,"z","s")







  3. #3
    Join Date
    Feb 2000
    Location
    Athens, Greece
    Posts
    137

    Re: howto overwrite a char in a string?!

    thanx for the reply.

    the problem with 'replace' is that it would replace every matching string, i.e.

    Dim s as string
    s = "this is i test"
    s(9) = "a" 'would be nice, should produce "this is a test"
    s = replace (s, "i", "a") 'would give "thas as a test".. ;-)




    more suggestions?


  4. #4
    Join Date
    Feb 2000
    Location
    Ireland
    Posts
    808

    Re: howto overwrite a char in a string?!

    That's true
    But how are you going to determine the logic deciding what you want to replace in a string
    if you want to break a string up into an array then use the following code

    Dim strMyString() as string
    Dim StringToSplit as string
    dim i as long
    StringToSplit = "This is A Test"

    for i = 1 to len(StringToSplit)
    ReDim Preserve strMyString(i - 1)
    strMyString(i - 1) = mid(StringToSplit, i, 1)
    next i





  5. #5
    Join Date
    Apr 2000
    Posts
    4

    Re: howto overwrite a char in a string?!

    I use this routine i wrote.



    private Function ReplaceInString(MainString as string, Pos as Long, RepString as string) as string
    ReplaceInString = Left(MainString, Pos - 1) & RepString & Right(MainString, len(MainString) - Pos)
    End Function


    'this then gives
    dim Mystring,Out1,Out2

    MyString = "This iz i test"
    Out1=ReplaceInString(MyString, 9, "a") 'this iz a test
    out2=ReplaceInString(MyString, 7, "s") 'this is i test








    Hope this helps

    Peter


  6. #6
    Join Date
    Feb 2000
    Location
    Athens, Greece
    Posts
    137

    Re: howto overwrite a char in a string?!

    thanx peter,

    yeah this looks like the solution to plan B, "how to do it if all else fails".

    I guess you didnt find anything standard either?
    i was hoping there would've been some easier solution, sigh, i think i'll use this routine.

    thanx for the suggestion.


  7. #7
    Join Date
    Apr 1999
    Location
    Rotterdam, Netherlands
    Posts
    278

    Re: howto overwrite a char in a string?!

    Hmmm maybe I missed something, but hmm doesn't

    Dim s as string
    s = "blafblah"
    mid$(s, 4, 1) = "h"



    work?

    Crazy D :-)
    "One ring rules them all"

  8. #8
    Join Date
    Feb 2000
    Location
    Athens, Greece
    Posts
    137

    Re: howto overwrite a char in a string?!

    yup.. that did it..

    yes, that would be the -right in front of me- solution that i overlooked ;-)

    Mid(string, start[, length])

    The Mid function syntax has these named arguments:

    string Required. String expression from which characters are returned. If string contains Null, Null is returned.

    Somuch for the documentation


    thanx


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured