title: How to remove invalid code points from a string?
body: <p>I have a routine that needs to be supplied with normalized strings.  However, the data that's coming in isn't necessarily clean, and String.Normalize() raises ArgumentException if the string contains invalid code points.</p>
  
  <p>What I'd like to do is just replace those code points with a throwaway character such as '?'.  But to do that I need an efficient way to search through the string to find them in the first place.  What is a good way to do that?</p>
  
  <p>The following code works, but it's basically using try/catch as a crude if-statement so performance is terrible.  I'm just sharing it to illustrate the behavior I'm looking for:</p>
  
  <pre><code>private static string ReplaceInvalidCodePoints(string aString, string replacement)
  {
      var builder = new StringBuilder(aString.Length);
      var enumerator = StringInfo.GetTextElementEnumerator(aString);
  
      while (enumerator.MoveNext())
      {
          string nextElement;
          try { nextElement = enumerator.GetTextElement().Normalize(); }
          catch (ArgumentException) { nextElement = replacement; }
          builder.Append(nextElement);
      }
  
      return builder.ToString();
  }
  </code></pre>
  
  <p>(edit:) I'm thinking converting the text to UTF-32 so that I could quickly iterate over it and see if each dword corresponds to a valid code point.  Is there a function that will do that?  If not, is there a list of invalid ranges floating around out there?</p>