none
Is there any method to Findout Hyphen in a string? RRS feed

  • Question

  • Hello msdn,

    Is there any library method for checking whether the character is Hyphen or Not? Like the one char.IsLetterOrDigit().


    salampv.07

    Friday, January 11, 2013 5:56 AM

Answers

  • For hyphens and dashes in different languages, consider GetUnicodeCategory as well. This is a test:

    bool b;
    b = char.GetUnicodeCategory('-') == UnicodeCategory.DashPunctuation;
    b = char.GetUnicodeCategory('\u2010') == UnicodeCategory.DashPunctuation; // hyphen
    b = char.GetUnicodeCategory('\u2011') == UnicodeCategory.DashPunctuation; // non-breaking hyphen
    b = char.GetUnicodeCategory('\u2012') == UnicodeCategory.DashPunctuation; // figure dash
    b = char.GetUnicodeCategory('\u2013') == UnicodeCategory.DashPunctuation; // en dash
    b = char.GetUnicodeCategory('\u2014') == UnicodeCategory.DashPunctuation; // em dash
    b = char.GetUnicodeCategory('\u2015') == UnicodeCategory.DashPunctuation; // horizontal bar
    b = char.GetUnicodeCategory('\u2212') == UnicodeCategory.DashPunctuation; // minus

    • Proposed as answer by Mike FengModerator Monday, January 14, 2013 9:10 AM
    • Marked as answer by salam.p.v Wednesday, January 16, 2013 11:24 AM
    Friday, January 11, 2013 1:34 PM

All replies

  • You can compare any char to any literal char using equals (as one of many ways)

          char c = '-';
          
    bool trueFalse = c.Equals('-');
          c = 
    'a';
          trueFalse = c.Equals(
    '-');

    • Edited by JohnWein Friday, January 11, 2013 6:21 AM
    Friday, January 11, 2013 6:14 AM
  • For hyphens and dashes in different languages, consider GetUnicodeCategory as well. This is a test:

    bool b;
    b = char.GetUnicodeCategory('-') == UnicodeCategory.DashPunctuation;
    b = char.GetUnicodeCategory('\u2010') == UnicodeCategory.DashPunctuation; // hyphen
    b = char.GetUnicodeCategory('\u2011') == UnicodeCategory.DashPunctuation; // non-breaking hyphen
    b = char.GetUnicodeCategory('\u2012') == UnicodeCategory.DashPunctuation; // figure dash
    b = char.GetUnicodeCategory('\u2013') == UnicodeCategory.DashPunctuation; // en dash
    b = char.GetUnicodeCategory('\u2014') == UnicodeCategory.DashPunctuation; // em dash
    b = char.GetUnicodeCategory('\u2015') == UnicodeCategory.DashPunctuation; // horizontal bar
    b = char.GetUnicodeCategory('\u2212') == UnicodeCategory.DashPunctuation; // minus

    • Proposed as answer by Mike FengModerator Monday, January 14, 2013 9:10 AM
    • Marked as answer by salam.p.v Wednesday, January 16, 2013 11:24 AM
    Friday, January 11, 2013 1:34 PM
  • Hi,

    if there is no any direct method that you want in library then you can define extension method on that type.

    namespace Extensions
    {
        public static class Extension
        {
            public static bool IsHyphen(this char x)
            {
                if(x.Equals('-'))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }

    Hope this helps.

    One good question is equivalent to ten best answers.

    Friday, January 11, 2013 2:15 PM