ttrogden
asked on
how to replace a single character
Does anyone have a routine or have a suggestion on how to replace a single character - embedded within a string with a space or another character
The following code will replace all occurrences of "c" in s$ to spaces
s$="abcabcabcabcabc"
startpos=1
do while true
thispos=instr(startpos,s$, "c")
if thispos = 0 then exit do
s$=left(s$,thispos-1) & " " & mid(s$,thispos+1) ' replace c with space
startpos=thispos+1
loop
s$="abcabcabcabcabc"
startpos=1
do while true
thispos=instr(startpos,s$,
if thispos = 0 then exit do
s$=left(s$,thispos-1) & " " & mid(s$,thispos+1) ' replace c with space
startpos=thispos+1
loop
If you are using VB 6 use the replace function
Replace(<entire string>,<search string>, <replace string>)
eg
msgbox Replace("123456123111221", "12", "Z")
will show a message box with a text value of "Z3456Z311Z21"
Replace(<entire string>,<search string>, <replace string>)
eg
msgbox Replace("123456123111221",
will show a message box with a text value of "Z3456Z311Z21"
mark is right but :
marker = instr( source$, anychar$ )
if marker > 1 then
source$ = left( source, marker -1 ) & Youcharachter &right( source, len( source ) - marker )
endif
marker = instr( source$, anychar$ )
if marker > 1 then
source$ = left( source, marker -1 ) & Youcharachter &right( source, len( source ) - marker )
endif
ASKER
sorry but i posted this question for my boss and he said that sbmc's answers worked for him the best - thanks guys -
There is a slightly simpler syntax for this:
Public Sub Replace(sSource As String, sTarget As String, sFill As String)
Dim Pos As Long
Pos = InStr(sSource, sTarget)
Do
If (Pos > 0) Then
Mid$(sSource, Pos, 1) = sFill
End If
Pos = InStr(sSource, sTarget)
Loop While Pos > 0
End Sub
(If you want to be really careful, you would want to check that sTarget and sFill are only one character)
MD
Public Sub Replace(sSource As String, sTarget As String, sFill As String)
Dim Pos As Long
Pos = InStr(sSource, sTarget)
Do
If (Pos > 0) Then
Mid$(sSource, Pos, 1) = sFill
End If
Pos = InStr(sSource, sTarget)
Loop While Pos > 0
End Sub
(If you want to be really careful, you would want to check that sTarget and sFill are only one character)
MD
Try this
srchstr is the string in which character "t" is replaced by "d"
Mid(srchstr, InStr(1, a, "t"), 1) = "d"
srchstr is the string in which character "t" is replaced by "d"
Mid(srchstr, InStr(1, a, "t"), 1) = "d"
ASKER
how do i assign points to sbmc??
Reject any one else's answer, and ask sbmc to resubmit their comment as an answer.
Have to agree with sbmc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if marker > 1 then
source$ = left( source, marker -1 ) & right( source, len( source ) - marker )
endif