Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 557173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:59:53+00:00 2026-05-13T11:59:53+00:00

Strings are usually enumerated by character. But, particuarly when working with Unicode and non-English

  • 0

Strings are usually enumerated by character. But, particuarly when working with Unicode and non-English languages, sometimes I need to enumerate a string by grapheme. That is, combining marks and diacritics should be kept with the base character they modify. What is the best way to do this in .Net?

Use case: Count the distinct phonetic sounds in a series of IPA words.

  1. Simplified definition: There is a one-to-one relationship between a grapheme and a sound.
  2. Realistic definition: Special “letter-like” characters should also be included with the base character (ex. pʰ), and some sounds may be represented by two symbols joined by a tie bar (k͡p).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:59:53+00:00Added an answer on May 13, 2026 at 11:59 am

    Simplified scenario

    The TextElementEnumerator is very useful and efficient:

    private static List<SoundCount> CountSounds(IEnumerable<string> words)
    {
        Dictionary<string, SoundCount> soundCounts = new Dictionary<string, SoundCount>();
    
        foreach (var word in words)
        {
            TextElementEnumerator graphemeEnumerator = StringInfo.GetTextElementEnumerator(word);
            while (graphemeEnumerator.MoveNext())
            {
                string grapheme = graphemeEnumerator.GetTextElement();
    
                SoundCount count;
                if (!soundCounts.TryGetValue(grapheme, out count))
                {
                    count = new SoundCount() { Sound = grapheme };
                    soundCounts.Add(grapheme, count);
                }
                count.Count++;
            }
        }
    
        return new List<SoundCount>(soundCounts.Values);
    }
    

    You can also do this using a regular expression: (From the documentation, the TextElementEnumerator handles a few cases that the expression below does not, particularly supplementary characters, but those are pretty rare, and in any case not needed for my application.)

    private static List<SoundCount> CountSoundsRegex(IEnumerable<string> words)
    {
        var soundCounts = new Dictionary<string, SoundCount>();
        var graphemeExpression = new Regex(@"\P{M}\p{M}*");
    
        foreach (var word in words)
        {
            Match graphemeMatch = graphemeExpression.Match(word);
            while (graphemeMatch.Success)
            {
                string grapheme = graphemeMatch.Value;
    
                SoundCount count;
                if (!soundCounts.TryGetValue(grapheme, out count))
                {
                    count = new SoundCount() { Sound = grapheme };
                    soundCounts.Add(grapheme, count);
                }
                count.Count++;
    
                graphemeMatch = graphemeMatch.NextMatch();
            }
        }
    
        return new List<SoundCount>(soundCounts.Values);
    }
    

    Performance: In my testing, I found that the TextElementEnumerator was about 4 times as fast as the regular expression.

    Realistic scenario

    Unfortunately, there is no way to “tweak” how the TextElementEnumerator enumerates, so that class will be of no use in the realistic scenario.

    One solution is to tweak our regular expression:

    [\P{M}\P{Lm}]      # Match a character that is NOT a character intended to be combined with another character or a special character that is used like a letter
    (?:                # Start a group for the combining characters:
      (?:                # Start a group for tied characters:
        [\u035C\u0361]      # Match an under- or over- tie bar...
        \P{M}\p{M}*         # ...followed by another grapheme (in the simplified sense)
      )                  # (End the tied characters group)
      |\p{M}             # OR a character intended to be combined with another character
      |\p{Lm}            # OR a special character that is used like a letter
    )*                 # Match the combining characters group zero or more times.
    

    We could probably also create our own IEnumerator<string> using CharUnicodeInfo.GetUnicodeCategory to regain our performace, but that seems like too much work to me and extra code to maintain. (Anyone else want to have a go?) Regexes are made for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I need to find some strings inside DB usually it is just host-name
Regular Expressions are usually expressed as strings, but they also have properties (ie. single
Possible Duplicate: Why is string concatenation faster than array join? Usually we need to
I usually place my keys on an xml and access them with R.string.key_name but
C is always pretty awkward with strings, but it's usually okay to just allocate
Python loves raising exceptions, which is usually great. But I'm facing some strings I
While working with COM in C++ the strings are usually of BSTR data type.
In python to print a formatted string with ints and strings, I'd usually do
I am trying to compare two variables in which are usually strings. These variables
I usually use JSON with jQuery to just return a string with html. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.