NMKP
NMKP

Reputation: 799

Split a string with special character in VBScript

I have a string with multiple special character, I want it to be split it with the special characters.

Example:

s = "apple+onion*abc/ki&jk"

Output:

apple
onion
abc
ki
jk

Upvotes: 2

Views: 4542

Answers (3)

Sebastin Anthony
Sebastin Anthony

Reputation: 660

This also one of the method to split the string..

Dim s:s="apple+onion*abc/ki&jk"

ReDim words(-1)


  For Each line In Split(s,"+")
   For Each linetwo In Split(line,"*")
    For Each linethree In Split(linetwo,"/")

     For Each linefour In Split(linethree,"&")

         ReDim Preserve words(UBound(words)+1)
         words(UBound(words))=linefour

     Next

    Next
   Next
  Next


for i=0 to 4 step 1
    Msgbox(words(i))
Next

Upvotes: 1

Barney
Barney

Reputation: 1895

Another way of doing, this is just slightly changing the regular expression

strPhrase = "apple+onion*abc/ki&jk"

Set objRegEx = CreateObject("vbscript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.Pattern = "[^a-z0-9]"
ReplacedText = objRegEx.Replace(strPhrase, " ")

objArr = Split(ReplacedText)
For i = 0 To UBound(objArr)
  If Trim(objArr(i)) <> "" Then
    Debug.Print objArr(i)
  End If
Next

Set objRegEx = Nothing

Upvotes: 2

Hackoo
Hackoo

Reputation: 18837

You can make it like this way :

Dim s : s = "apple+onion*abc/ki&jk"
Dim r : Set r = New RegExp
r.Global = True
r.Pattern = "\+|\*|&|®|@|/"
NewString = r.Replace(s,"_")
wscript.echo NewString 
Tab = Split(NewString,"_")
For i=LBound(Tab) to Ubound(Tab)
    Msg = Msg & Tab(i) & vbCrlf
Next
wscript.echo Msg

Upvotes: 3

Related Questions