|
-
Sep 20th, 2010, 05:27 PM
#1
Thread Starter
Member
Count how many "comma's" are in a line.
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.
-
Sep 20th, 2010, 05:33 PM
#2
Re: Count how many "comma's" are in a line.
you can just split the string with the comma as the delimiter and check the array length.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Sep 20th, 2010, 05:41 PM
#3
Thread Starter
Member
Re: Count how many "comma's" are in a line.
Im splitting the line like this. Dim parts As New List(Of String)(Regex.Split(RichTextBox2.Text, ", ?")
How do I check the array length?
-
Sep 20th, 2010, 05:47 PM
#4
Re: Count how many "comma's" are in a line.
No.
try this
Code:
Dim Count As Integer = RichTextBox.Text.Split(New Char() {","c}).Length
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Sep 20th, 2010, 08:19 PM
#5
Re: Count how many "comma's" are in a line.
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()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 21st, 2010, 12:41 AM
#6
Re: Count how many "comma's" are in a line.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Sep 21st, 2010, 02:09 AM
#7
Re: Count how many "comma's" are in a line.
 Originally Posted by motil
No.
try this
Code:
Dim Count As Integer = RichTextBox.Text.Split(New Char() {","c}).Length
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
-
Sep 21st, 2010, 03:38 AM
#8
Re: Count how many "comma's" are in a line.
Why the character "c" is used in your code ? 
Note: I'm a newbie
Last edited by akhileshbc; Sep 21st, 2010 at 03:46 AM.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 21st, 2010, 05:53 AM
#9
Re: Count how many "comma's" are in a line.
 Originally Posted by akhileshbc
Why the character " c" is used in your code ?
Note: I'm a newbie 
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#:
csharp Code:
string str = "X";
char ch = 'X';
In VB:
vb.net Code:
Dim str As String = "X"
Dim ch As Char = "X"c
-
Sep 21st, 2010, 06:19 AM
#10
Re: Count how many "comma's" are in a line.
Thanks jmcilhinney 
I have created a separate thread regarding this question : http://www.vbforums.com/showthread.php?t=628219
Sorry for hijacking the thread.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
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
|