Get Only Left Integers?

# Thread: Get Only Left Integers?

1. Member
Join Date
Dec 1969
Posts
45

## Get Only Left Integers?

Hi,<BR> Is there a way to tell VBScript to only grab the leftmost integers? I&#039;ve got integers ranging in length from 2-3 and I need the script to grab them and not the rest of the string which is text and numbers.<BR>Example:<BR>123time832<BR>I want to strop time832 off and only keep 123.<BR>Respectfully,<BR>David.

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

## Various ways

How about good old #1: Brute force:<BR><BR>&#060;%<BR>txt = "123time832"<BR>num = "?"<BR>For i = 1 To Len(txt)<BR> If Not IsNumeric( Mid(txt,i,1) ) Then<BR> If i = 1 Then <BR> num = 0 &#039; no digits at all found<BR> Else<BR> num = CLNG( Left( txt, i-1 ) ) <BR> End If<BR> Exit For<BR> End If<BR>Next<BR>If num = "?" Then <BR> &#039; must not have found any non-digit in txt<BR> num = CLNG( txt ) &#039; so *all* are digits<BR>End If<BR>%&#062;<BR><BR>Could also do something with regular expressions.<BR><BR>Surely various other ways.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
1,846

## Well if it's always 2 or 3 you could just do

Dim StrChum<BR>Dim IntChum<BR><BR>StrChum = "123time832"<BR><BR>If IsNumeric(Left(StrChum,3)) Then<BR> IntChum = CInt(Left(StrChum,3))<BR><BR>ElseIf IsNumeric(Left(StrChum,2)) Then<BR> IntChum = CInt(Left(StrChum,2))<BR>Else<BR> IntChum = 0<BR>End If<BR><BR>Response.Write(IntChum)<BR><BR>

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

## Ahhh...that gives me another way...

&#060;%<BR>txt = "123time832"<BR><BR>num = 0<BR>For i = Len(txt) To 1 Step -1<BR> If IsNumeric( Left( txt, i ) ) Then<BR> num = CLNG( Left( txt, i ) )<BR> Exit For<BR> End If<BR>Next<BR>%&#062;<BR><BR>Hmmm???<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
1,846

/

6. Member
Join Date
Dec 1969
Posts
45

## RE: Ahhh...that gives me another way...

Bill,<BR> Okay...I don&#039;t want to just cut and paste your code into the script without understanding it...and I think I understand almost everything...but I&#039;m foggy on two things.<BR>1:Why does it say Len(txt) To 1 Step -1 instead of Len(txt) Step - 1?<BR>2: Why do you use the CLNG function? CLNG rounds a number to a whole number, correct?

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

## Good questions

1:Why does it say Len(txt) To 1 Step -1 instead of Len(txt) Step - 1?<BR><BR>Because that&#039;s the proper syntax for a FOR statement.<BR> FOR i = Len(txt) STEP -1<BR>is illegal. Go look it up in the VBScript Reference (link in left panel of this very page).<BR><BR>I wanted i to have the values 9,8,7,6,5,4,3,2,1 in order, so I started with Len(txt) and went *backwards* (using Step -1) to 1.<BR><BR>Hence <BR> For i = Len(txt) To 1 Step -1<BR>reads<BR> "loop, starting with i at the length of the string, <BR> changing the value of i by -1 each time through the loop,<BR> until you reach an ending value for i of 1."<BR><BR>************<BR><BR>2: Why do you use the CLNG function? CLNG rounds a number to a whole number, correct?<BR><BR>Correct. And if you aren&#039;t expecting or don&#039;t want any numbers with decimal points in them, you probably want to use CLNG. But if you want/expect decimal points then certainly use CDBL instead.<BR><BR><BR>

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

## And yet ANOTHER WAY...

...using a regular expression, as advertised, if anybody cares:<BR><BR>*********<BR>&#060;HTML&#062;&#060;B ODY&#062;<BR><BR>&#060;%<BR>Function getLeftNum( txt )<BR> Dim re, temp<BR> Set re = New RegExp<BR> re.Pattern = "D[wW]*\$"<BR> temp = re.Replace( txt, "" )<BR> If isNumeric( temp ) Then <BR> getLeftNum = CLNG(temp)<BR> Else<BR> getLeftNum = 0<BR> End If<BR>End Function<BR><BR>Sub test( txt )<BR> Response.Write "getLeftNum(" & txt & ") is " & getLeftNum(txt) & "&#060;P&#062;"<BR>End Sub<BR><BR>test "123test898"<BR>test "123456"<BR>test "nada887"<BR>test ""<BR><BR>%&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<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
•