|
-
Mar 25th, 2003, 01:25 PM
#1
Thread Starter
Frenzied Member
string
I have a string and need to get the last five characters out of it. I'm using:
lblCardNo.Text = Right$(empid, 5)
but the Right function doesn't seem to work in .NET
-
Mar 25th, 2003, 01:47 PM
#2
You can use the substring method of the string class:
VB Code:
Dim empid As String
TextBox1.Text = empid.Substring(empid.Length - 5, 5)
-
Mar 25th, 2003, 02:28 PM
#3
Thread Starter
Frenzied Member
thanks Edneeis..that worked...I also found this to work too:
'lblCardNo.Text = Microsoft.VisualBasic.Right(empid, 5)
in your example, what do the -5, and 5 define? I'm assuming the negative tells it to start at the end of the string and the 5 grabs the number of characters, but why two fives?
TextBox1.Text = empid.Substring(empid.Length - 5, 5)
-
Mar 25th, 2003, 03:00 PM
#4
1st param is the start index, the 2nd is the length. So if you want the last 5 char then you go to the end and count backwards. You may be able to leave the length out if you want the rest of the string, I'm not sure on that one though.
-
Mar 25th, 2003, 03:23 PM
#5
PowerPoster
Just a note, if and/or when .Net apps can be ran on other platforms, the Microsoft namespace probably won't be able to be used. It would be better to use the substring method if you can. Of course, if you don't care about the portability of your code in the case it would be possible to run on other platforms, then don't worry about it.
-
Mar 25th, 2003, 03:27 PM
#6
Thread Starter
Frenzied Member
thanks Hell, I did go with the SubString method.
another question. I have a string: 1111111111111111
I want to make it credit card friendly: 1111-1111-1111-1111
How do I chop up the string to place a dash in there?
-
Mar 25th, 2003, 03:48 PM
#7
Well SubString works just like VB6s Mid Function or if the number is stored as something other than a string you can format it in the ToString method.
VB Code:
Dim ccn As Long = 111111111
MsgBox(ccn.ToString("###-##-####"))
-
Mar 25th, 2003, 11:00 PM
#8
Fanatic Member
or you could just use a regular expression to do the job for you.
I built a nice little cc validation class using regex and I have used it many many times over.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|