Results 1 to 8 of 8

Thread: string

  1. #1

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can use the substring method of the string class:

    VB Code:
    1. Dim empid As String
    2.         TextBox1.Text = empid.Substring(empid.Length - 5, 5)

  3. #3

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    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)

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  6. #6

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    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?

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Dim ccn As Long = 111111111
    2.         MsgBox(ccn.ToString("###-##-####"))

  8. #8
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    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
  •  



Click Here to Expand Forum to Full Width