Results 1 to 4 of 4

Thread: Count Characters left of a hyphen

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Count Characters left of a hyphen

    I have a series of job numbers like this

    K3444-
    K5633-
    3434-
    3434- A
    3434- B
    M3456-

    I need to count the characters left of the dash and then add a leading space to those that only have 4 characters. It should look something like this:

    K3444-
    K5633-
      3434-
      3434- A
      3434- B
    M3456-

    So far (with the help of another thread on this forum) I have been able to figure out how to count all the characters in the textbox and add the spacer with this:
    VB Code:
    1. Dim strCharCount As String = txtJobNumber.Text
    2.         strCharCount = RTrim(strCharCount)
    3.         Dim iLength As Integer = strCharCount.Length
    4.         If iLength >= 5 Then
    5.             strCharCount = strCharCount
    6.         ElseIf iLength = 4 Then
    7.             strCharCount = " " & strCharCount
    8.         ElseIf iLength <= 3 Then
    9.             MsgBox("You must use a valid Job Number.", _
    10.                 MsgBoxStyle.OKOnly, "Length Error!")
    11.             txtJobName.Clear()
    12.             txtJobNumber.Focus()
    13.             Exit Sub
    14.         End If
    How do I modify this to count only the characters left of the - (dash)?
    Last edited by BukHix; Jan 5th, 2004 at 02:37 PM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    You could Split() the string on the dash as delimiter, giving you an array element like myArray(0) which would contain all characters to the left of the dash. Or as you read in the characters, check them for a dash, but this would probably be less efficient.

  3. #3
    Hyperactive Member jovton's Avatar
    Join Date
    Nov 2000
    Location
    South Africa
    Posts
    266
    Use the "InStr" function:
    VB Code:
    1. Dim charCount As Integer = Instr(MyString, "-") - 1
    Or use the "IndexOf" method of the string class:
    VB Code:
    1. Dim charCount As Integer = MyString.IndexOf("-")
    When -1 is returned, the expression was not found.
    Last edited by jovton; Jan 5th, 2004 at 03:19 PM.
    jovton

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Thanks to both of you for the reply. I tried it both ways and with a lot of trial and error I finaly resolved it using the Split that salvelinus mentioned.

    Just in case someone else would like to see an example of the finished Sub it looks like this:
    VB Code:
    1. Dim strCharCount As String = RTrim(txtJobNumber.Text)
    2.         Dim arrCharCount() As String = strCharCount.Split("-")
    3.         Dim iLength As Integer = arrCharCount(0).Length
    4.  
    5.         If iLength >= 5 Then
    6.             strCharCount = strCharCount
    7.             txtJobNumber.Text = strCharCount
    8.         ElseIf iLength = 4 Then
    9.             strCharCount = " " & strCharCount
    10.             txtJobNumber.Text = strCharCount
    11.         ElseIf iLength <= 3 Then
    12.             MsgBox("You must use a valid Job Number.", _
    13.                 MsgBoxStyle.OKOnly, "Length Error!")
    14.             txtJobName.Clear()
    15.             txtJobNumber.Focus()
    16.             Exit Sub
    17.         End If

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