Results 1 to 10 of 10

Thread: Count how many "comma's" are in a line.

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    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.

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    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?

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    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

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Count how many "comma's" are in a line.

    or you could use LINQ:

    vb Code:
    1. Dim str As String = "1,2,3,4,5"
    2. Dim Count As Integer = Aggregate c As Char In str _
    3.                                 Where c = "," _
    4.                                 Into Count()

  6. #6
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Count how many "comma's" are in a line.

    slower :P
    * 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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Count how many "comma's" are in a line.

    Quote Originally Posted by motil View Post
    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Count how many "comma's" are in a line.

    Quote Originally Posted by akhileshbc View Post
    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:
    1. string str = "X";
    2. char ch = 'X';
    In VB:
    vb.net Code:
    1. Dim str As String = "X"
    2. Dim ch As Char = "X"c
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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
  •  



Click Here to Expand Forum to Full Width