Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip Numbers off Right Side of String 3

Status
Not open for further replies.

ajap99

Programmer
Dec 9, 2004
18
GB
I need to strip the numbers off the right hand side of as string as far as the = sign

Example Joe Bloggs RID=1234
I need the 1234 from above

I can get the last digit from code below

lbl3.text=Right(""& e.Item.Cells(1).text &" ",2)
Above will display the last digit i.e. 3

The lenth after the = sign varies

Do I need a loop to return these values then
Can anyone suggest the loop

Thanks
 
Hi,

No need for the loop. Try something like:
Code:
lbl3.text = Right(Item.Cells(1).text, Len(Item.Cells(1).text) - InStr(1, Item.Cells(1).text, "=", 1))

Instr will find the first occurance of the "=" in the string and your are then taking the right value of the length of the string minus the position of the "=".

i.e. the string RID=1234 will return 1234.

Hope this helps

Harleyquinn

---------------------------------
For tsunami relief donations
 
Or the shorter (but perhaps more obscure):

lbl3.text = Split(Item.Cells(1).text, "=")(1)
 
strongm,
strongm said:
shorter (but perhaps more obscure):
Perhaps it is, but I like it. I always seem to forget about it for tasks like this and go with Len() and Instr() (as you can see above). It can be very useful that Split() function...

Harleyquinn

---------------------------------
For tsunami relief donations
 
If there is only one equals sign in each string, you can get the characters to the right of this by using the InStr function.

Here is an example which you can create easily in an Access or VB form. Create a form with a command button (btnTest) and a text box (txtTest1). Add this code to the On_Click event of the button:
Code:
Private Sub btnTest_Click()

Dim intPos As Integer
Dim strResult As String

intPos = InStr(txtTest1, "=")

If intPos > 0 Then
    strResult = Right$(txtTest1, Len(txtTest1) - intPos)
    MsgBox strResult
Else
    MsgBox "no = found in text"
End If

End Sub

Type a test string into the text box, e.g. Joe Bloggs RID=1234

You will see the result 1234 in the message box.

I hope that this example will help you to see how to use InStr ... if your data can contain more than one '=' sign, then post some examples and I can try to modify the code to suit.


Bob Stubbs
 
Bob,

Did I not demonstrate (albeit assuming the data would always have a "=" in) the use of Right(), Len() and Instr() in my first post?

Harleyquinn

---------------------------------
For tsunami relief donations
 
Harleyquinns code works OK
But I have 3 = signs in the string
eg string is 7Class=9p Teacher=Sean Bloggs RID=7
And I want the contents after the last = (above is 7)
 
Then instead of using instr you should use instrRev.
It does the same thing but starts searching at the end of the string...

--------------------------------------
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs,
and the Universe trying to produce bigger and better idiots.
So far, the Universe is winning.
 
ajap99 Cheers for the star, glad to be a help.

strongm The Split() function was a good call in my eyes, have a
star.gif
from me.

Harleyquinn

---------------------------------
For tsunami relief donations
 
For multiple occurrences of '=' use a modified version of strongm's code:

lbl3.text = Split(Item.Cells(1).text, "=")(ubound(Split(Item.Cells(1).text),"="))

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top