CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2004
    Posts
    158

    Splitting text from numbers.

    Is there a .split command that would allow me to separate numbers from text in a given string.

    Mystring = "1234, this, is, text,5678"

    Using the split command I would just like for

    this
    is
    text

    to be returned ignoring the numbers. Is this possible?
    I use VB.Net 2008 in all my wash, for those whiter whites.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Splitting text from numbers.

    Hello again!

    You see, you having a need to discard the numbers throws a spanner in the works, but it is not too complicated

    We first need to split the string - I have noticed that there is a comma separating each segment, so we can use the comma as a delimiter. Right, after the split, we would have to test whether or not that segment is numeric or not. If it isn't numeric, then we add it to the label for example :

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim MyString As String
            Dim arrSplit()
            MyString = "1234, this, is, text,5678"
    
            arrSplit = MyString.Split(",") 'Split at comma
    
            For i As Integer = 0 To arrSplit.Length - 1 ' loop through each segment
                If Not IsNumeric(arrSplit(i)) Then 'if it isn't numeric
                    Label1.Text = Label1.Text & arrSplit(i) 'add to label
                End If
            Next
        End Sub
    I hope it helps

  3. #3
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: Splitting text from numbers.

    Use Regular Expression to strip the numbers out of the text. If you need the numbers as well, you can just use different groups to capture them.

    For example:
    Code:
    Dim Mystring As String = "1234, this, is, text,5678"
    Dim regEx As New Regex("\d+")
    Dim Mystring2 As String = regEx.Replace(Mystring, "")
    Mystring2 will then contain ", this, is, text" and can be split and trimmed.
    Last edited by Alsvha; May 25th, 2010 at 01:30 AM.

  4. #4
    Join Date
    Apr 2004
    Posts
    158

    Re: Splitting text from numbers.

    Hi!

    Thank you so much, that really helps, and written in a way that even a fool like me may understand.

    As a point of interest, I have noticed this in some examples...

    arrSplit = MyString.Split(","c) 'Split at comma

    Could you tell me the meaning of the "C" after ","

    Thank you.
    I use VB.Net 2008 in all my wash, for those whiter whites.

  5. #5
    Join Date
    Apr 2004
    Posts
    158

    Re: Splitting text from numbers.

    Quote Originally Posted by Alsvha View Post
    Use Regular Expression to strip the numbers out of the text. If you need the numbers as well, you can just use different groups to capture them.

    For example:
    Code:
    Dim Mystring As String = "1234, this, is, text,5678"
    Dim regEx As New Regex("\d+")
    Dim Mystring2 As String = regEx.Replace(Mystring, "")
    Mystring2 will then contain ", this, is, text" and can be split and trimmed.
    Oooh, this one is going to take some thinking about.
    I use VB.Net 2008 in all my wash, for those whiter whites.

  6. #6
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: Splitting text from numbers.

    Quote Originally Posted by dajunka View Post
    Hi!

    Thank you so much, that really helps, and written in a way that even a fool like me may understand.

    As a point of interest, I have noticed this in some examples...

    arrSplit = MyString.Split(","c) 'Split at comma

    Could you tell me the meaning of the "C" after ","

    Thank you.
    The "c" IIRC means character and you do not really need it.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Splitting text from numbers.

    Ah, Regex At the end of the day it would be the most "appropriate" way

    I tend to avoid them, as my regex skills aren't very good

    Good one Alsvha!

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

Featured