|
-
January 18th, 2000, 12:43 AM
#1
VBScript Help - Stripping characters from string
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
-
January 18th, 2000, 02:08 AM
#2
Re: VBScript Help - Stripping characters from string
use the Replace function in VBScript.
newstring = replace(oldstring, ",", "")
newstring = replace(newstring, "'", "")
-
January 18th, 2000, 06:08 PM
#3
Re: VBScript Help - Stripping characters from string
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
-
January 18th, 2000, 06:16 PM
#4
Re: VBScript Help - Stripping characters from string
I didn't notice until I had already posted that you said VBScrip and not VB...Oops!!
Sorry,
Rippin
-
January 19th, 2000, 02:50 PM
#5
Re: VBScript Help - Stripping characters from string
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
John Pirkey
MCSD (VB6)
http://www.stlvbug.org
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|