Results 1 to 7 of 7

Thread: Extracting and printing defined characters from a substring

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Angry Extracting and printing defined characters from a substring

    I wish to print into a label the first 3 characters from a text box called txtSurname.text, and a string variable called surname. The current code looks like

    Code:
    lblRef.Caption = surname & Int(Rnd * 100)
    As it is, the full surname prints in the label followed by a random number between 1 and 99. I wish it to print the first 3 letters of the surname followed by the random number.

    As far as I know this uses substrings. Could anybody show me how to do this.

    The label is lblRef.caption the surname is txtSurname.text

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I wish it to print the first 3 letters of the surname followed by the random number.
    VB Code:
    1. lblRef.Caption = cstr(left(surname,3)) & CInt(Rnd * 100)

    The substring is used in vb.net, in vb5/vb6, you need to use the left(), mid(), and right functions.
    Last edited by alex_read; Dec 2nd, 2004 at 08:32 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    VB Code:
    1. lblRef.Caption = Left$(surname, 3) & (Int(Rnd * 99) + 1)

    Left$ takes the given number of characters out of the string and returns these. The edited Rnd code will result the 1 - 99 you mentioned, because Rnd returns a random number from 0 to anything below 1. Thus adding one makes sure 1 is the lowest score you can get.

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    followed by a random number between 1 and 99
    Ah, not quite! This will generate a number from 0 to 99. Use this instead:

    VB Code:
    1. CInt((Rnd * 99) + 1)

    This will generate a random number from 0 to 98, then add a 1 to it!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    CInt uses bankers' rounding, so it might even give results up to the given number.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Command1.Caption = CInt(Rnd * 2)
    3. End Sub

    See for yourself! Returns 0, 1 or 2

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    ah, ok, didn't know that one! kiitos paljon!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16
    Thx for all your help, worked perfectly. Now onto this part:

    http://www.vbforums.com/showthread.p...hreadid=314667

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