PDA

View Full Version : Find a number in a string



Lancill
03-04-2003, 10:34 AM
Hi, how can I return the index of the number substring in alphanumeric strings?
Ex:
hello145hello
I need to return the start and the end position of 145

Bye

Worm
03-04-2003, 10:44 AM
Do you know before hand what the numbers are? Or are you wanting something like an IsNumeric to tell if its alpha or numeric, and then start counting until an alpha is found again?

Lancill
03-04-2003, 02:32 PM
No, I don't know what's the number in the string. I need to read a random string and return only the numeric value in the string.
Ex2: HE34590LLO
Numeric Value: 34590

kpsmith
03-04-2003, 02:47 PM
I don't know of an easy way but you could try something like this.

Get the complete string.
Get the total length of the string

Get the first character of the string
If it is >= 0 keep it and check the next character
Else get the total string minus the first character

REPEAT until you checked the entire string for digits.

Worm
03-04-2003, 03:17 PM
Try this:

<pre>
%TestString% = "HE34590LLO"
%Length% = String.GetLength ("%TestString%")
%Ctr% = "0"
%HIT% = "FALSE"

WHILE (%Ctr% < %Length%)
%SubString% = String.Mid ("%TestString%", %ctr%, 1)
%IsZero% = Evaluate (%Substring% * 0)
IF (%IsZero% = 0)
IF (%HIT% = "FALSE")
%FirstDigit% = "%Ctr%"
%HIT% = "TRUE"
END IF
ELSE
IF (%HIT%="TRUE")
%LastDigit% = Evaluate (%Ctr%)
%NumberOfDigits% = Evaluate (%LastDigit% - %FirstDigit%)
%NumericValue% = String.Mid ("%TestString%", % FirstDigit%, %NumberOfDigits%)
%Result% = Dialog.MessageBox ("Title", "The numeric value in the strin...", Ok, Question)
%Ctr% = "%Length%"
END IF
END IF
%Ctr% = Evaluate (%Ctr% + 1)
END WHILE</pre>

Cut and Paste Version Here (http://www.warmuskerken.com/ams/findnumeric.txt)

Lancill
03-05-2003, 09:25 AM
ok thx! /ubbthreads/images/icons/smile.gif