?
Solved

how to replace a single character

Posted on 1999-01-25
11
Medium Priority
?
146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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