| | Hi, I am trying to find some simple code that can get the numerical values from a string of letters and numbers. Say the string was 'a296g064j17r', how would I get the 29606417 out to use somewhere else? Thanks, -jimmy |
|
|
The only way i can think of is using regular expression. I'm assuming you're using VB.Net. .Net has regular expression built in. You can use it to replace all the non numeric characters. Either that, a less professional way, is to loop through all the char, and only keep those within 30h to 39h or 48d to 57d.
I'm not that good with regex, but if you can't figure out how to go about, i can help come out with short code, later. Good Luck OK, to get to the serious stuff, this is how I solve your problem in the Linux / UNIX / cygwin shell, as a prerequisite for kind of understanding the concept, after which I will talk about how you need to approach the problem in a mainstream programming language: CONSOLE # echo "a296g064j17r" | sed 's#[^0-9]##g' 29606417 # sed is the stream editor in UNIX, it's the standard command line way to perform regular expression substitutions. The 's' flag stands for 'substitute', and the convention is: SEPARATORinput-string-you-want-to-replaceSEPARATORoutput-string-that-will-replace-stuffSEPARATOR The separator could be anything, I just chose the hash mark based on a personal preference. Before I explain what [^0-9] is, let me explain what [0-9] is - it's kind of a wildcard, but whereas the asterisk means ANY character, the [0-9] wildcard means any digit, or otherwise all characters whose ASCII representation is greater that that of '0' and less than that of '9'. By the way, POSIX-compliant regular expression engines give you the option of putting the string [:digit:] instead of the string 0-9, like this: CONSOLE # echo "a296g064j17r" | sed 's#[^[:digit:]]##g' 29606417 # Now, the caret sign just simply puts a negation in front of that, so [^0-9] is a wildcard regular expression that will match all non-didgit characters. But the fact that you have posted this topic in the programming section of the forum doesn't make it obvious which way you're going. Some programming languages (like Java and PHP) do have regular expression capabilities, while other languages don't. Heck, you even have regular expression engines built into some popular databases like MySQL and Oracle. Oracle, for instance, has an SQL function called REGEXP_REPLACE that will allow you to do this kind of stuff. So whichever database platform or programming language you're exploring, look in the docs for a section called "Regular expression capabilities", and if there is one, I can almost guarantee you that you will find a replace function that takes regular expressions as the first argument.
Or you could analyse one letter at a time and check if it is a digit. If it is, you just insert it into a new string, which you can convert to a number after going through the original string. It might not be as elegant as using regular expressions, but it's less complicated.
Or you could analyse one letter at a time and check if it is a digit. If it is, you just insert it into a new string, which you can convert to a number after going through the original string. It might not be as elegant as using regular expressions, but it's less complicated. Ah, simplicity. I tend to agree with this sort of approach, as while it may not be as efficient as other methods unless you need to keep the resources your program uses to a bare minimum it's far quicker and less irritating. Add to that the fact that doing a process as you would do it as a human (going through and copying all of the digits to somewhere else) means that it flows more naturally in the mind and makes it one hell of a lot easier to debug when you're reading through it again.
Recent Queries:-
Keywords : converting alphanumeric strings numerical values
|
|
![]() Converting Alphanumeric Strings Into Numerical Values |
| ADD REPLY / Got an Opinion! | a humble request :-) | RAPID SEARCH! | Free Hosting | [X] |
|
Express your Opinions, Thoughts or Contribute your information that might help someone here. Ask your Doubts & Queries to get answers.. "Together, We enlight each other!" |
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP. | 500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE |
|