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
Printable View
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
You can use the substring method of the string class:
VB Code:
Dim empid As String TextBox1.Text = empid.Substring(empid.Length - 5, 5)
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)
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.
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.
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?
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("###-##-####"))
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.