Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Replace Nth character in a string

Reply
 
Old 05-29-2009   #1 (permalink)
Heinz


 
 

Replace Nth character in a string

Hello,

is it possible to replace a character at a arbitrary position in a string ?

For example :
I want to replace the third and hundredth character of the variable myText
with an * :

So bascically something like this:
mid(myText, 3,1) = "*"
mid(myText, 100,1) = "*"

I had a look at the "replace" function... but this needs a character to
find.... but I have only a position that I need to replace.

This is what I have tried :

[01]Function cleanString (sString)
[02] '** replace each character having ASC-Value <31 with "*" - execpt 10
and 13 (linefeed)
[03] for i = 1 to len (sString)
[04] iCharToCheck = asc ( mid (sString,i,1 ) )
[05] if iCharToCheck < 31 and ( iCharToCheck <> 10 and iCharToCheck
<> 13) then
[06] '** i = position to replace :
[07] sString = left(sString, i-1) & "*" & mid(sString, i+1,999)
'** concat everthing left and right from the occurance - place "*"
in-between
[08] wscript.echo "fixed " & mid (sString,i,1) & "@" & i
[09]
[10] end if
[11]
[12] next
[13]
[14] cleanString = sString
[15]
[16] end function

But this replaces the first occurence only - and then I get :

"invalid procedure call or argument: 'asc' " in line 2


thank you



My System SpecsSystem Spec
Old 05-29-2009   #2 (permalink)
Heinz


 
 

Re: Replace Nth character in a string

small mistake in my posting:

"invalid procedure call or argument: 'asc' " in line 4


"Heinz" <Spacewalker4711(noSpam)@hotmail.com> schrieb im Newsbeitrag
news:uvY6%23tG4JHA.5048@xxxxxx
Quote:

> Hello,
>
> is it possible to replace a character at a arbitrary position in a string
> ?
>
> For example :
> I want to replace the third and hundredth character of the variable
> myText with an * :
>
> So bascically something like this:
> mid(myText, 3,1) = "*"
> mid(myText, 100,1) = "*"
>
> I had a look at the "replace" function... but this needs a character to
> find.... but I have only a position that I need to replace.
>
> This is what I have tried :
>
> [01]Function cleanString (sString)
> [02] '** replace each character having ASC-Value <31 with "*" - execpt
> 10 and 13 (linefeed)
> [03] for i = 1 to len (sString)
> [04] iCharToCheck = asc ( mid (sString,i,1 ) )
> [05] if iCharToCheck < 31 and ( iCharToCheck <> 10 and iCharToCheck
> <> 13) then
> [06] '** i = position to replace :
> [07] sString = left(sString, i-1) & "*" & mid(sString,
> i+1,999) '** concat everthing left and right from the occurance - place
> "*" in-between
> [08] wscript.echo "fixed " & mid (sString,i,1) & "@" & i
> [09]
> [10] end if
> [11]
> [12] next
> [13]
> [14] cleanString = sString
> [15]
> [16] end function
>
> But this replaces the first occurence only - and then I get :
>
> "invalid procedure call or argument: 'asc' " in line 2
>
>
> thank you
>
>

My System SpecsSystem Spec
Old 05-29-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: Replace Nth character in a string


"Heinz" <Spacewalker4711(noSpam)@hotmail.com> wrote in message
news:uvY6%23tG4JHA.5048@xxxxxx
Quote:

> Hello,
>
> is it possible to replace a character at a arbitrary position in a string
> ?
>
> For example :
> I want to replace the third and hundredth character of the variable
> myText with an * :
>
> So bascically something like this:
> mid(myText, 3,1) = "*"
> mid(myText, 100,1) = "*"
>
> I had a look at the "replace" function... but this needs a character to
> find.... but I have only a position that I need to replace.
>
> This is what I have tried :
>
> [01]Function cleanString (sString)
> [02] '** replace each character having ASC-Value <31 with "*" - execpt
> 10 and 13 (linefeed)
> [03] for i = 1 to len (sString)
> [04] iCharToCheck = asc ( mid (sString,i,1 ) )
> [05] if iCharToCheck < 31 and ( iCharToCheck <> 10 and iCharToCheck
> <> 13) then
> [06] '** i = position to replace :
> [07] sString = left(sString, i-1) & "*" & mid(sString,
> i+1,999) '** concat everthing left and right from the occurance - place
> "*" in-between
> [08] wscript.echo "fixed " & mid (sString,i,1) & "@" & i
> [09]
> [10] end if
> [11]
> [12] next
> [13]
> [14] cleanString = sString
> [15]
> [16] end function
>
> But this replaces the first occurence only - and then I get :
>
> "invalid procedure call or argument: 'asc' " in line 2
>
>
> thank you
>
>
If you want to replace the 3rd and 100th characters with "*", then the Mid
function seems best. For example:

If (Len(strOld) > 100) Then
strNew = Left(strOld, 2) & "*" & Mid(strOld, 4, 96) & "*" & Mid(strOld,
101)
End If

If instead you want to replace all characters where the ASCII value is less
than 31 (except 10 and 13) with "*", then you need to look at every
character in the string. I would use:

For k = 1 To Len(strOld)
If (Asc(Mid(strOld, k, 1)) < 31) Then
If (Asc(Mid(strOld, k, 1)) <> 10) And (Asc(Mid(strOld, k, 1)) < >
13) Then
strOld = Left(strOld, k - 1) & "*" & Mid(strOld, k + 1)
End If
End If
Next

Your code looks similar, but I didn't get your error when I tested. I don't
know why, but I never use the extra spaces in the function calls, and I used
the default for the third parameter of the Mid function (which is the rest
of the string) when I don't know how long the rest of the string is.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 05-29-2009   #4 (permalink)
Heinz


 
 

Re: Replace Nth character in a string

thank you.. I've cut & paste your code and it works...

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> schrieb im
Newsbeitrag news:O0A7qOH4JHA.1420@xxxxxx
Quote:

>
> "Heinz" <Spacewalker4711(noSpam)@hotmail.com> wrote in message
> news:uvY6%23tG4JHA.5048@xxxxxx
Quote:

>> Hello,
>>
>> is it possible to replace a character at a arbitrary position in a string
>> ?
>>
>> For example :
>> I want to replace the third and hundredth character of the variable
>> myText with an * :
>>
>> So bascically something like this:
>> mid(myText, 3,1) = "*"
>> mid(myText, 100,1) = "*"
>>
>> I had a look at the "replace" function... but this needs a character to
>> find.... but I have only a position that I need to replace.
>>
>> This is what I have tried :
>>
>> [01]Function cleanString (sString)
>> [02] '** replace each character having ASC-Value <31 with "*" - execpt
>> 10 and 13 (linefeed)
>> [03] for i = 1 to len (sString)
>> [04] iCharToCheck = asc ( mid (sString,i,1 ) )
>> [05] if iCharToCheck < 31 and ( iCharToCheck <> 10 and
>> iCharToCheck <> 13) then
>> [06] '** i = position to replace :
>> [07] sString = left(sString, i-1) & "*" & mid(sString,
>> i+1,999) '** concat everthing left and right from the occurance - place
>> "*" in-between
>> [08] wscript.echo "fixed " & mid (sString,i,1) & "@" & i
>> [09]
>> [10] end if
>> [11]
>> [12] next
>> [13]
>> [14] cleanString = sString
>> [15]
>> [16] end function
>>
>> But this replaces the first occurence only - and then I get :
>>
>> "invalid procedure call or argument: 'asc' " in line 2
>>
>>
>> thank you
>>
>>
>
> If you want to replace the 3rd and 100th characters with "*", then the Mid
> function seems best. For example:
>
> If (Len(strOld) > 100) Then
> strNew = Left(strOld, 2) & "*" & Mid(strOld, 4, 96) & "*" & Mid(strOld,
> 101)
> End If
>
> If instead you want to replace all characters where the ASCII value is
> less than 31 (except 10 and 13) with "*", then you need to look at every
> character in the string. I would use:
>
> For k = 1 To Len(strOld)
> If (Asc(Mid(strOld, k, 1)) < 31) Then
> If (Asc(Mid(strOld, k, 1)) <> 10) And (Asc(Mid(strOld, k, 1)) < >
> 13) Then
> strOld = Left(strOld, k - 1) & "*" & Mid(strOld, k + 1)
> End If
> End If
> Next
>
> Your code looks similar, but I didn't get your error when I tested. I
> don't know why, but I never use the extra spaces in the function calls,
> and I used the default for the third parameter of the Mid function (which
> is the rest of the string) when I don't know how long the rest of the
> string is.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Modify string based on a specific character VB Script
Re: String manipulation using replace etc. VB Script
regular expressions to replace but keep character? VB Script
search and replace string PowerShell
How do I escape the wildcard character in a path string? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46