|
-
Jan 5th, 2004, 02:26 PM
#1
Thread Starter
Hyperactive Member
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:
Dim strCharCount As String = txtJobNumber.Text
strCharCount = RTrim(strCharCount)
Dim iLength As Integer = strCharCount.Length
If iLength >= 5 Then
strCharCount = strCharCount
ElseIf iLength = 4 Then
strCharCount = " " & strCharCount
ElseIf iLength <= 3 Then
MsgBox("You must use a valid Job Number.", _
MsgBoxStyle.OKOnly, "Length Error!")
txtJobName.Clear()
txtJobNumber.Focus()
Exit Sub
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.
-
Jan 5th, 2004, 02:58 PM
#2
Frenzied Member
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.
-
Jan 5th, 2004, 03:11 PM
#3
Hyperactive Member
Use the "InStr" function:
VB Code:
Dim charCount As Integer = Instr(MyString, "-") - 1
Or use the "IndexOf" method of the string class:
VB Code:
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
-
Jan 5th, 2004, 04:54 PM
#4
Thread Starter
Hyperactive Member
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:
Dim strCharCount As String = RTrim(txtJobNumber.Text)
Dim arrCharCount() As String = strCharCount.Split("-")
Dim iLength As Integer = arrCharCount(0).Length
If iLength >= 5 Then
strCharCount = strCharCount
txtJobNumber.Text = strCharCount
ElseIf iLength = 4 Then
strCharCount = " " & strCharCount
txtJobNumber.Text = strCharCount
ElseIf iLength <= 3 Then
MsgBox("You must use a valid Job Number.", _
MsgBoxStyle.OKOnly, "Length Error!")
txtJobName.Clear()
txtJobNumber.Focus()
Exit Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|