Solved

how to replace a single character

Posted on 1999-01-25
11
142 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 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