Splitting a Number String

# Thread: Splitting a Number String

1. May
Member
Join Date
Dec 1969
Posts
42

## Splitting a Number String

Let&#039s say I have a number string, say &#039 1234&#039.<BR><BR>How do I split it up and assign each integer to a variable? <BR><BR>Example: I want to assign &#039 1&#039 to variable1, &#039 2&#039 to variable2 and so on.<BR><BR>Or can I do it by array? <BR><BR>Thanks.

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Splitting a Number String

You *should* do it into an array.<BR><BR>But why do you need to? You can always treat a string as if it were an array:<BR><BR>&#060;%<BR>str = "8713"<BR>digit4 = Mid( str, 4, 1 )<BR>%&#062;<BR><BR>and digit4 will then contain "3", because that is the 4th character of the string.<BR><BR>HOWEVER...if you really want to do this...<BR><BR>&#060;%<BR>str = "871263182211" &#039 or whatever and however you get it<BR>Dim digits( )<BR>ReDim digits( Len(str)-1 ) &#039 make array the right size<BR>For i = 0 To Len(str)-1<BR>&nbsp; &nbsp; digits(i) = Mid( str, i+1, 1 )<BR>*** OR ***<BR>&nbsp; &nbsp; digits(i) = CInt( Mid( str, i+1, 1 ) )<BR>Next<BR>%&#062;<BR><BR>Note that arrays in VBS *always* have an element number zero, and the code I used above reflects that. However, just becuz the element is there doesn&#039t mean you have to use it. If you&#039d prefer to ignore it, you could write that code as:<BR><BR>&#060;%<BR>str = "871263182211" &#039 or whatever and however you get it<BR>Dim digits( )<BR>ReDim digits( Len(str) ) &#039 make array the right size<BR>For i = 1 To Len(str)<BR>&nbsp; &nbsp; digits(i) = Mid( str, i, 1 )<BR>*** OR ***<BR>&nbsp; &nbsp; digits(i) = CInt( Mid( str, i, 1 ) )<BR>Next<BR>%&#062;<BR><BR>Finally, you would use the CInt function to convert the one-digit strings into true numbers. If you don&#039t care if they are still strings (instead of integer numbers) then you don&#039t need the call to CInt. Hence the "OR" choices above.<BR><BR>

3. Junior Member
Join Date
Dec 1969
Posts
24

## you can use split function in the vbscript

To split a string, we should have a delimiters(&#039,&#039 or &#039.&#039 or any delimiter)<BR> then use res=split(yourstring,delimiter), which returns an array, which has all data with splitted by commas <BR>example: yourstring="1,2,3,4"<BR>res=split(yourstring,",")< BR>then res(0)=1<BR> res(1)=2<BR> res(2)=3<BR> res(3)=4<BR>okey <BR>thats all in know<BR>

4. Senior Member
Join Date
Dec 1969
Posts
487

## RE: Splitting a Number String

Something like this would probably work:<BR><BR><BR>Dim number<BR>Dim int<BR>Dim counter<BR><BR><BR>number = CStr(123456)<BR>int = len(number)<BR><BR>Dim arrStrings(int)<BR><BR>for counter = 1 to int<BR><BR>arrStrings(counter) = Left(number, 1)<BR>number = Mid(number, 2)<BR><BR>next<BR><BR><BR>HTH<BR><BR>Darren<BR>[ darren@showusyourcode.com ]

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## But *ONLY* if there are separators...

...in his request, he didn&#039t show any characters separating the digits. And if there are no separators, then SPLIT cannot be used.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## A bug in that code!

Darren wrote:<BR><BR>***** QUOTE *****<BR>number = CStr(123456)<BR>int = len(number)<BR>Dim arrStrings(int)<BR>*******************<BR><BR>OOPS ! You cannot use a variable to DIM another variable!<BR><BR>You *MUST* use ReDim to do that!<BR><BR>As in:<BR><BR>***** QUOTE *****<BR>number = CStr(123456)<BR>int = len(number)<BR>Dim arrStrings()<BR>ReDim arrStrings(int)<BR>*******************<BR><BR>The use of Left and Mid in the loop is cute! It&#039s slower than simply using Mid, alone, but it is a technique worth remembering for other purposes.<BR><BR><BR> <BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•