12
votes

Is there any way by which we can get each character from a string using VBScript? I had used the Mid function but I just want to know if there are any other direct functions which when used returns each character starting from a string.

6

6 Answers

29
votes
strString = "test"
For i=1 To Len(strString)
    WScript.Echo Mid(strString,i,1)
Next 
5
votes
a="abcd"

for i=1 to len(a)

msgbox right(left(a,i),1)

next
4
votes

AFAIK, Mid is the only way to do this.

2
votes

Another way to do it, starting from 0 :

str = "hola che"
x=Len(str)    
text = ""
For i=0 to x-1  'x-1 is because it exceeds the actual length    
    text= text & Mid(str,i+1,1)    
Next    
msgbox text
1
votes

This code is useful to split Ucase and Lcase

Dim a
a="StAcKoVeRfLoW"

for i=o to len(a)-1
if mid(a,i+1,1)=ucase(mid(a,i+1,1)) then
  b=mid(a,i+1,1)
msgbox b
end if
next
0
votes

This works for me. LEFT and then RIGHT....

'Ugandan National Identity Number (NIN) has 14 digits
strFullNIN = "18650929392010"
strNIN_1 = LEFT(strFullNIN,1)
strNIN_2 = RIGHT(LEFT(strFullNIN,2),1)
strNIN_3 = RIGHT(LEFT(strFullNIN,3),1)
strNIN_4 = RIGHT(LEFT(strFullNIN,4),1)
strNIN_5 = RIGHT(LEFT(strFullNIN,5),1)
strNIN_6 = RIGHT(LEFT(strFullNIN,6),1)
strNIN_7 = RIGHT(LEFT(strFullNIN,7),1)
strNIN_8 = RIGHT(LEFT(strFullNIN,8),1)
strNIN_9 = RIGHT(LEFT(strFullNIN,9),1)
strNIN_10 = RIGHT(LEFT(strFullNIN,10),1)
strNIN_11 = RIGHT(LEFT(strFullNIN,11),1)
strNIN_12 = RIGHT(LEFT(strFullNIN,12),1)
strNIN_13 = RIGHT(LEFT(strFullNIN,13),1)
strNIN_14 = RIGHT(LEFT(strFullNIN,14),1)

If I didn't know the length of the initial string, I would do as follows:

strFullNIN = RS.fields("Client_NIN")
strFullNIN_LENGTH = LEN(strFullNIN)

x = 1
DO UNTIL x = strFullNIN_LENGTH
IF x = 1 THEN
strNIN_"& x &" = LEFT(strFullNIN,x)
ELSE
strNIN_"& x &" = RIGHT(LEFT(strFullNIN,x),1)
END IF
x=x+1
LOOP

Hope this is helpful to someone!