|
-
June 9th, 2000, 06:42 AM
#1
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
-
June 9th, 2000, 06:50 AM
#2
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")
-
June 9th, 2000, 06:55 AM
#3
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?
-
June 9th, 2000, 07:04 AM
#4
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
-
June 9th, 2000, 07:10 AM
#5
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
-
June 9th, 2000, 07:23 AM
#6
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.
-
June 9th, 2000, 08:16 AM
#7
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"
-
June 9th, 2000, 08:38 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|