Find the Last letter in a string containing letters and numbers
HI
I am trying to find the last letter within a string.
I have a field which contains data like CV8 7TY0124 121212
I am trying to extract everything prior to the first letter, ie CV8 7TY
I am thinking about finding the last letter within a string, then using the right, left or Mid funtion to grab the characters I want. Am I on the right lines?
Re: Find the Last letter in a string containing letters and numbers
Ok it seem's to me like its a zip code if so you could mid$ (string,1,7). But if you want the rest after the last letter being
example string = CV8 7TY
Last letter = Y<---------------------------
then what you do is you Mid$(String,7,Len(string)) but that's only if you want letters or numbers after the CV8 7TY. Please tell me if that's what you wanted because your post is not exactly clear.
P.S Hope i helped you
Coding's a Breeze if you'r at ease! GOD THATS CORNY!! -
Re: Find the Last letter in a string containing letters and numbers
I am trying to extract everything prior to the first letter, ie CV8 7TY
I assume you mean "prior to the last letter"
It would be faster using InstrRev and checking for the first character which isn't a space or number (Val) then using Left. (assuming you don't want to return a fixed length string. Is it a car number plate?)
Last edited by schoolbusdriver; Jun 2nd, 2006 at 05:21 AM.
Re: Find the Last letter in a string containing letters and numbers
Here's a one liner if you are sure the last character won't be a "0" (zero), otherwise modify appropriately:
VB Code:
Function ExtractLastNum(ByVal sIn As String) as String
ExtractLastNum = StrReverse(Val(StrReverse(sIn)))
End Function
Private Sub Command1_Click()
''usage
MsgBox ExtractLastNum("CV8 7TY0124 121212")
End Sub
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Re: Find the Last letter in a string containing letters and numbers
If I wanted to parse a string looking for the last number, or letter I'd probably go with a regular expression. RegExps can be more system intensive than other string parsing but they are also more powerfull.
If you wanted to use this approach you would start by adding a reference to MS VBScript Regular Expressions 5.5 and then try this (untested off the top of my head) code:
VB Code:
Dim Reg as New Regular Expression
Dim m as Match
Reg.IgnoreCase
Reg.pattern = "^(.*[a-z])[^a-z]"
for Each m in Reg.Execute(testString)
' m.submatches(0) should be the the string through the last letter
next m
If you want to return only the last letter move the parenthesis in the pattern so that the pattern would be "^.*([a-z])[^a-z]"
So you'll understand that a little the reg pattern can be interpeted as saying:
From the start of the string take any number of characters of any type (.*) until a letter is found ([a-z]) which is follwed by anything other than another letter ([^a-z]).
If you want to make it case sensetive remove the ignorecase command and use the appropiately cased a-z. You can even mix and match by using stuff like "[a-zB]" which would match all lower case letters and a uppercase B, etc.
While I'm here, if you are reading files and wan't to parse them a line a time add a "$" to the end of the pattern as the $ is interpetted as "end of line"
What bug? That's not a bug. It's an undocumented feature!
Re: Find the Last letter in a string containing letters and numbers
Originally Posted by anotherVBnewbie
RegExps can be more system intensive than other string parsing but they are also more powerfull.
They maybe more powerful, but how much power is really needed in a situation like this. It's kinda like trying to hammer a nail with a steamroller - it's just going to slow you down.
Attached is a speed test comparing my code with the RegExp one - whilst they performed equally in the IDE, once compiled the 'built-in' code was 40% faster than the RegExp stuff.
RegExp can be very useful but, for simple things like this, the standard function calls are often faster and of course don't require any overhead.
Re: Find the Last letter in a string containing letters and numbers
I agree with Bushmobile - using a FOR loop and stepping backwards through the string is an age-old standard algorithm for doing something like this.
If you were coding it in ASM you would do exactly the same thing.
I would have checked for 0 through 9 and space myself - but then you never indicated what would happen if a non-letter/non-numeric character was in place - such as a # or @ sign.
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
Re: Find the Last letter in a string containing letters and numbers
Originally Posted by szlamany
I would have checked for 0 through 9 and space myself - but then you never indicated what would happen if a non-letter/non-numeric character was in place - such as a # or @ sign.
That's the reason i chose to check for letters
but 0-9 & space would be quicker - also you'd use a byte array if you were looking for more speed
Re: Find the Last letter in a string containing letters and numbers
Originally Posted by schoolbusdriver
At 2Ghz+, bush, who cares! Easily understood + flexible. Nice.
Although faster chips are great - speed of CPU is not keeping up with requirement for speed on the part of the user.
Everytime you learn an algorithm, technique or construct and appreciate why it's this-or-that, fast-or-slow, memory-hog-or-not - that makes you a better programmer in the long run.
I've been doing this for a very long time - back to when your program had to fit in 16K of memory.
Every incremental step that CPU, memory and disk size took in the past 20 years was accompanied by a user-thirst for power and speed that surpassed what was just delivered.
Sorry for the rant - but this thread seemed to be all about different ways to attack a simple problem
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
Re: Find the Last letter in a string containing letters and numbers
Originally Posted by szlamany
Sorry for the rant - but this thread seemed to be all about different ways to attack a simple problem
No, don't apologise, you're right to rant...just as long as everyone accepts that as long as each method does the job the poster requested then no-one has given a wrong answer and people giving different possibilities is good for the poster as they have many different methods they can try :-)
I myself started programming on a Commodore +4 which coincidentally only had 16k of memory, so I know where you're coming from...a lot of my coding is about trying to improve speed and do things efficiently...in fact, before learning VB properly about a year ago (I was previously unable to wrap my head around how it all went together, but I eventually managed it) I was writing code in QBX...and I am sure many others here also started or came though a QuickBasic route to get here with VB :-)
Re: Find the Last letter in a string containing letters and numbers
Originally Posted by schoolbusdriver
At 2Ghz+, bush, who cares! Easily understood + flexible. Nice.
You'd care if you had millions of them to process
in this case using a byte array makes it four times faster than using a string.
Originally Posted by szlamany
Everytime you learn an algorithm, technique or construct and appreciate why it's this-or-that, fast-or-slow, memory-hog-or-not - that makes you a better programmer in the long run.
Re: Find the Last letter in a string containing letters and numbers
I join in just for the fun of giving competition in the speed market ^_^
VB Code:
Public Function BeforeNumbers(ByRef Text As String) As String
Dim lngA As Long, lngB As Long
For lngA = Len(Text) To 1 Step -1
lngB = AscW(Mid$(Text, lngA, 1))
If (lngB = 32& Or (lngB >= 48& And lngB <= 57&)) Then Else Exit For
Next lngA
If lngA > 1 Then BeforeNumbers = Left$(Text, lngA)
End Function
You can do faster of course, but I still set simplicity, shortness and elegancy over the speed in this one (thus no byte array either) I chose the "check for numbers and spaces" line, but it would only be a matter of either adding more detection for special characters or then just setting the alphabets. It all depends on the case: can there be other alphabets besides the English ones?
Speedwise this code is about two times faster than bushmobile's when compiled. Main reasons being: comparisons stringwise is slow and Select Case is slow.
I jumped to VB from C64. I remember it being weird I couldn't slow down a program by doing For...Next loops
Life is cruel sometimes Four times faster. This is optimization from the other end compared to my last post: usage becomes harder, but the results speedwise are impressive. This optimization can be counted as "insane" for the task it is done for.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering