Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split String Into Alpha & Numeric Components

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I need to split a string into its alpha and numeric components. The text will look something like "EMPU123456" with the first 3 or 4 characters being alpha and the rest being numeric. For those of you curious these are thepiggyback semi-trailer numbers you see loaded on rail cars. The following code works, just wondering if there's a more efficient method? I know. I could start at the 4th character, but left it at one for testing purposes.
[code/]
Dim ColNo As Integer
Dim NumColNo As Integer = 0
Dim cTrlrAlpha As String = ""
Dim cTrlrNum As String = ""
For ColNo = 1 To txtTrailerNo.Text.Length
NumColNo = InStr(1, "01234567890", Mid(txtTrailerNo.Text, ColNo, 1).ToString)
If NumColNo > 0 Then
cTrlrAlpha = Mid(txtTrailerNo.Text, 1, ColNo - 1).ToString.Trim
cTrlrNum = Mid(txtTrailerNo.Text, ColNo).ToString.Trim
Exit For
End If
Next
[/code]

Auguy
Sylvania, Ohio
 
I've used a simular function to yours in the past, but a used IsNumeric to find the numbers (rather than InStr).
 
I'm not that good with regular expressions but you might want to try this.
Code:
        Dim myMatches As MatchCollection
        Dim myRegex As New Regex("\d+")
        Dim successfulMatch As Match
        myMatches = myRegex.Matches(TextBox1.Text)
        successfulMatch = myMatches(0)
        Dim index As Integer = successfulMatch.Index
        TextBox2.Text = TextBox1.Text.Substring(0, index)
        TextBox3.Text = TextBox1.Text.Substring(index, TextBox1.Text.Length - index)
 
Thanks to both of you. I thought of isnumeric after I posted the question. I'll keep Wayne's suggestion on file, might work better on longer strings.

Auguy
Sylvania, Ohio
 
Hello,

Regular Expressions are perfect for this kind of thing.
Code:
Imports System.Text.RegularExpressions

Private Sub SplitData(ByVal text As String, _
                      ByRef textPortion As String, _
                      ByRef numericPortion As Integer)
    Dim pattern As String = "([A-Z]+)(\d+)"
    If Regex.IsMatch(text, pattern) Then
        Dim m As Match = Regex.Match(text, pattern)
        textPortion = m.Groups(1).ToString
        numericPortion = Integer.Parse(m.Groups(2).ToString)
    End If
End Sub
The pattern says match any upper case letter one or more times and save it into back reference 1. Then match one or more numerals and save it into back reference 2. Since match sub groups are objects you have to cast them to their respective types when using them. Sub group 0 always returns the entire match. Good Luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top