I need a code to count how many commas there are in a richtextbox line. If there are 4 or more comma's then do something, else delete line.
Im not sure if regex is required for this.
Printable View
I need a code to count how many commas there are in a richtextbox line. If there are 4 or more comma's then do something, else delete line.
Im not sure if regex is required for this.
you can just split the string with the comma as the delimiter and check the array length.
Im splitting the line like this. Dim parts As New List(Of String)(Regex.Split(RichTextBox2.Text, ", ?")
How do I check the array length?
No.
try this
Code:Dim Count As Integer = RichTextBox.Text.Split(New Char() {","c}).Length
or you could use LINQ:
vb Code:
Dim str As String = "1,2,3,4,5" Dim Count As Integer = Aggregate c As Char In str _ Where c = "," _ Into Count()
slower :P
There's no need to explicitly create a Char array unless you want to specify a StringSplitOptions too. The overload that takes just a Char array declares its parameter as a ParamArray, meaning it will accept zero, one or more discrete Chars as well as a single array:Code:Dim Count As Integer = RichTextBox.Text.Split(","c).Length
Why the character "c" is used in your code ? :confused:
Note: I'm a newbie :blush:
Zero or more characters within double quotes is a String literal. Exactly one character in double quotes and suffixed with a lower case c is a Char literal. In C#:In VB:csharp Code:
string str = "X"; char ch = 'X';vb.net Code:
Dim str As String = "X" Dim ch As Char = "X"c
Thanks jmcilhinney :wave:
I have created a separate thread regarding this question : http://www.vbforums.com/showthread.php?t=628219
Sorry for hijacking the thread.:p