Click to See Complete Forum and Search --> : howto overwrite a char in a string?!


mihalidis
June 9th, 2000, 06:42 AM
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

TH1
June 9th, 2000, 06:50 AM
Use the Replace function in VB6

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

mihalidis
June 9th, 2000, 06:55 AM
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?

TH1
June 9th, 2000, 07:04 AM
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

Peter McDonnell
June 9th, 2000, 07:10 AM
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

mihalidis
June 9th, 2000, 07:23 AM
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.

Crazy D
June 9th, 2000, 08:16 AM
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"

mihalidis
June 9th, 2000, 08:38 AM
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