Click to See Complete Forum and Search --> : Splitting text from numbers.
dajunka
May 25th, 2010, 12:58 AM
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?
HanneSThEGreaT
May 25th, 2010, 01:20 AM
Hello again! :wave:
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 :
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 :)
Alsvha
May 25th, 2010, 01:26 AM
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:
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.
dajunka
May 25th, 2010, 01:33 AM
Hi! :wave:
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.
dajunka
May 25th, 2010, 01:36 AM
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:
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. :)
Alsvha
May 25th, 2010, 01:39 AM
Hi! :wave:
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.
HanneSThEGreaT
May 25th, 2010, 02:00 AM
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 :blush:
Good one Alsvha! :thumb:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.