Count Dash Space and Apostrophe in string

Posted on 2011-10-20
Last Modified: 2012-05-12
I am in need of a function that when passed in a string it counts the dashes(-), spaces ( ) and apostrophe’s (‘) and returns a count.  
ie I pass in a string of:

RED-CAR        it would return 1
F O X Y      it would return 3
A-TX’N      it would return 2
MY 69’ GTO      it would return 3
Any great ideas?
Question by:us1975mc
    LVL 28

    Accepted Solution

    Public Function GetCount(ByVal val As String) As Integer
        return val.Split(" -'".ToCharArray()).Length 'You may need to subtract 1 here
    End Function
    LVL 74

    Expert Comment

    by:käµfm³d 👽

    int occurrences = System.Text.RegularExpressions.Regex.Matches(input, "[- ’]").Count;

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Change the "int occurrences" to "Dim occurrences As Integer" and remove the semi-colon in my example. I didn't notice that it was for VB  : \

    Author Closing Comment

    Sorry that it took so long.  I stepped out and just returned and it works fine with the -1.


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now