Help Parsing Strings? Is the given Character a Letter or a Number?

Started by mcjathan, July 09, 2015, 04:03:05 PM

Previous topic - Next topic

mcjathan

Hi all,  I'm looking for some suggestions how to build a function in Winbatch that accomplishes the following.  Let's call the function I want to build fred(string)

I have a list of individual strings that look something like this:

IAR
MISC
ST60-40
S70-30
SX100-0
ST0-100

I'm only interested in the strings that contain a dash.  So I can use StrIndex() function to identify strings that don't contain a hyphen and simply return a null string.

However, once I have the strings that do contain a hyphen (-) and want to grab the number prior to the hypen and I want to discard any letters prior to the number.  So for the strings above, here's what I need the function to return:

fred("IAR") = ""
fred("MISC") = ""
fred("ST60-40") = "60"
fred("S70-30") = "70"
fred("SX100-0") = "100"
fred("ST0-100") = "0"

So, once I know I have a string that contains a hyphen, I can easily throw away everything after (and including the hyphen).  However, I'm not sure how to discard the leading letters in the remaining portion of the string and leave only numbers.  Suggestions?  How can I start at the beginning of the remaining string and character-by-character determine if the character is a letter or a number?

Regards,

Jeff

....IFICantBYTE

How about this?

Code (winbatch) Select
#DefineFunction fred(StringIn)
Return StrClean(ItemExtract(1,StringIn,"-"),"0123456789","",@FALSE,2)
#EndFunction

message("",fred("IAR")); = ""
message("",fred("MISC")); = ""
message("",fred("ST60-40")); = "60"
message("",fred("S70-30")); = "70"
message("",fred("SX100-0")); = "100"
message("",fred("ST0-100")); = "0"

Exit
Regards,
....IFICantBYTE

Nothing sucks more than that moment during an argument when you realize you're wrong. :)

mcjathan

Efficient, elegant, beautiful!

I've done a respectable amount of programing in Winbatch but never used the StrClean function before.

Thanks, IfICantBYTE!

Regards,

Jeff

snowsnowsnow

The nitpicker in me wants to point out that this will fail (I.e., give the wrong answer) if you have an input string like:

S87FOO70-abc

which seems like it could be possible, down the road.

For that, you'll have to actually get down and parse the pieces of the string.

DAG_P6

Quote from: snowsnowsnow on July 11, 2015, 07:54:14 AM
The nitpicker in me wants to point out that this will fail (I.e., give the wrong answer) if you have an input string like:

S87FOO70-abc

which seems like it could be possible, down the road.

For that, you'll have to actually get down and parse the pieces of the string.

I must be missing something. Why would that fail?
David A. Gray
You are more important than any technology.