Click to See Complete Forum and Search --> : VBScript Help - Stripping characters from string


sdc
January 17th, 2000, 11:43 PM
Hello All,
I need help on the best way to strip characters from a string using VBScript.
The string might be something like this: ('word1','word2','word3','word4','word5'). I need to strip out the , and '.
I need to set each word to it's own variable when all the extra characters are stripped.
Thanks in advance,
SDC

Lothar Haensler
January 18th, 2000, 01:08 AM
use the Replace function in VBScript.
newstring = replace(oldstring, ",", "")
newstring = replace(newstring, "'", "")

Rippin
January 18th, 2000, 05:08 PM
I had a similar problem and solved it with this function. You pass it the string that has the characters you want to strip out, the character to replace the stripped characters with (if you don't want to replace the characters then pass ""), and the list of characters to strip out.

For example, if I have strStrip = "'Test1','Test2','Test3'" and I want to remove all of the single quotes and all of the commas I would call the function like this:


Dim strClean as string
Dim strStrip as string

strStrip = "'Test1','Test2','Test3'"

strClean = ReplaceInvalidChars(strStrip,"","'",",")

'strClean should now contain "Test1Test2Test3"




This will return the string, "Test1Test2Test3"
Since the InvaidChars parameter is a paramarray, you can pass as many characters as you need all in one function call.

Put this code in your app.


public Function ReplaceInvalidChars(byval Text as string, byval ReplacementChar as string, paramarray InvalidChars())
Dim strReturn as string
Dim vntChar as Variant

strReturn = Text
for Each vntChar In InvalidChars()

strReturn = Replace(strReturn, vntChar, ReplacementChar)

next vntChar

ReplaceInvalidChars = strReturn
End Function




Hope this helps,
Rippin

Rippin
January 18th, 2000, 05:16 PM
I didn't notice until I had already posted that you said VBScrip and not VB...Oops!!

Sorry,
Rippin

Johnny101
January 19th, 2000, 01:50 PM
You will use the replace function as mentioned by other posts, but this will only do half of what you are looking for. If your web server supports VBscript version 5, then you can do this:

(you can test this by trying a replace function, if it works, then this should work as well.)

1) strip out the single tick marks
sString = Replace(sSource,"'","")
now your string has no single tick marks.
2) split the string into seperate values.
The split function will return an array of values.
(
ex. in VB 6
Dim s() as String
dim t as string
t = "dog,cat,cow"

s = Split(t,",")
if you looped through the array starting with index 1 you would get:
dog
cat
cow
)
In VBScript:
Dim s
s = Split(sString,",")

now the variable "s" is an array with each entry being one of the values in the source string separated by the comma.
Now you can pull out the array values like this:

string1 = s(1)
string2 = s(2)
...

Hope this helps.

John

John Pirkey
MCSD
www.ShallowWaterSystems.com