Visual Basic 6 - Getting value before a "-"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LOCAFO
    New Member
    • Aug 2007
    • 6

    Visual Basic 6 - Getting value before a "-"

    I'm writing code in vb 6. I need to get the left value of a string before the "-'. What is the syntax to do this.

    Example:

    sTestNumber is a string and the value is "Parks-87364-2734". I want to save
    "Parks" into the string value sNameOnly.

    What is the code to accomplish this?

    Thanks,
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    Originally posted by LOCAFO
    I'm writing code in vb 6. I need to get the left value of a string before the "-'. What is the syntax to do this.

    Example:

    sTestNumber is a string and the value is "Parks-87364-2734". I want to save
    "Parks" into the string value sNameOnly.

    What is the code to accomplish this?

    Thanks,
    sNameOnly = sTestNumber.sub string(1,5)

    or

    i once used something like:
    sNameOnly = sTestNumber.sub string(1,charin dex("-"))
    but cant remember exactly how it worked.

    on the other hand this works...
    [code=vb.net]
    Dim i As Integer
    Dim a As String
    Dim sNameOnly As String
    Dim sTestNumber As String
    sTestNumber = "Parks-87364-2734"

    For i = 1 To Len(sTestNumber )
    a = Mid(sTestNumber , i, 1)
    If a <> "-" Then
    sNameOnly = sNameOnly + a
    Else
    'do something
    Exit Sub
    End If
    Next
    [/code]

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      Use combination of left and mid function. No need of loops here.

      Comment

      • jamesd0142
        Contributor
        • Sep 2007
        • 471

        #4
        Originally posted by debasisdas
        Use combination of left and mid function. No need of loops here.
        so how can i use the left and mid functions to get substring of "Parks-87364-2734" at the index of "-"???

        I cant work this out myself :P

        Comment

        • jamesd0142
          Contributor
          • Sep 2007
          • 471

          #5
          ok this works cheers for the guidence
          [code=vbnet]
          Dim sNameOnly As String
          Dim sTestNumber As String = "Parks-87364-2734"
          sNameOnly = sTestNumber.Sub string(0, (sTestNumber.In dexOf("-", 0) + 1) - 1)
          MsgBox(sNameOnl y)
          [/code]

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            That will not work in VB6. Try this conversion...

            [CODE=vb]Dim sNameOnly As String
            Dim sTestNumber As String
            sTestNumber = "Parks-87364-2734"
            sNameOnly = Left$(sTestNumb er, Instr(sTestNumb er, "-") - 1)
            MsgBox(sNameOnl y)[/CODE]
            Or, as an alternative...
            [CODE=vb]Dim sNameOnly As String
            Dim sTestNumber As String
            sTestNumber = "Parks-87364-2734"
            sNameOnly = Split(sTestNumb er, "-")(0)
            MsgBox(sNameOnl y)[/CODE]Note, I just typed these here, haven't tested either.

            Comment

            Working...