Solved

how to replace a single character

Posted on 1999-01-25
11
139 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:ttrogden
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 1470659
marker = instr( source$, anychar$ )
if marker > 1 then
   source$ = left( source, marker -1 ) & right( source, len( source ) - marker )
endif

0
 
LVL 2

Expert Comment

by:sbmc
ID: 1470660
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

0
 
LVL 3

Expert Comment

by:traygreen
ID: 1470661
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"
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1470662
mark is right but :
marker = instr( source$, anychar$ )
if marker > 1 then
   source$ = left( source, marker -1 ) & Youcharachter &right( source, len( source ) - marker )
endif


0
 
LVL 1

Author Comment

by:ttrogden
ID: 1470663
sorry but i posted this question for my boss and he said that sbmc's answers worked for him the best - thanks guys -


0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 18

Expert Comment

by:mdougan
ID: 1470664
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
0
 
LVL 1

Expert Comment

by:amithvs
ID: 1470665
Try this
srchstr is the string in which character "t" is replaced by "d"
Mid(srchstr, InStr(1, a, "t"), 1) = "d"
0
 
LVL 1

Author Comment

by:ttrogden
ID: 1470666
how do i assign points to sbmc??
0
 
LVL 18

Expert Comment

by:mdougan
ID: 1470667
Reject any one else's answer, and ask sbmc to resubmit their comment as an answer.
0
 

Expert Comment

by:DevlonDuthie
ID: 1470668
Have to agree with sbmc
0
 
LVL 2

Accepted Solution

by:
sbmc earned 20 total points
ID: 1470669
Now submitted as an answer. Thanks for your comments


       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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now