Solved

how to replace a single character

Posted on 1999-01-25
11
140 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 50
Restricting text box entry from \/:?<>"| 9 76
Excel VBA combine two working workbooks 8 46
I need help embedding an image as HTML in my vb.net application 3 45
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

930 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

10 Experts available now in Live!

Get 1:1 Help Now