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?
Printable View
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?
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 :
I hope it helps :)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
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:
Mystring2 will then contain ", this, is, text" and can be split and trimmed.Code:Dim Mystring As String = "1234, this, is, text,5678"
Dim regEx As New Regex("\d+")
Dim Mystring2 As String = regEx.Replace(Mystring, "")
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.
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: