|
-
May 25th, 2010, 12:58 AM
#1
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.
-
May 25th, 2010, 01:20 AM
#2
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
-
May 25th, 2010, 01:26 AM
#3
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.
-
May 25th, 2010, 01:33 AM
#4
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.
-
May 25th, 2010, 01:36 AM
#5
Re: Splitting text from numbers.
 Originally Posted by Alsvha
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.
-
May 25th, 2010, 01:39 AM
#6
Re: Splitting text from numbers.
 Originally Posted by dajunka
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.
-
May 25th, 2010, 02:00 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|