Solved

how to replace a single character

Posted on 1999-01-25
11
144 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

707 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