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 123969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:22:16+00:00 2026-05-11T04:22:16+00:00

I’ve got a simple procedure to strip a string of all characters illegal in

  • 0

I’ve got a simple procedure to strip a string of all characters illegal in XML:

string SanitizeXml(string xml) {     return string.Concat         (xml.ToCharArray().Where(c => IsLegalXmlChar(c)).ToArray()); } 

It’s nice and terse. But I’m concerned about its performance. The same thing could easily be accomplished using a simple for loop:

string SanitizeXml(string xml) {     var buffer = new StringBuilder();      foreach(char c in xml)     {         if (IsLegalXmlChar(c))         {             buffer.Append(c);         }     }      return buffer.ToString(); } 

What sticks out to me is that, in the second example, xml is converted to a char[], and Where()’s IEnumerable<char> back to a char[]. I seem to do this a lot with LINQ–change between arrays and enumerables.

Should I be concerned about this? What kind of performance hit am I going to get, in general, for relying on LINQ extension methods when there a clear alternative that may be a little more verbose.

Perhaps this is too broad of a question.

  • 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. 2026-05-11T04:22:17+00:00Added an answer on May 11, 2026 at 4:22 am

    Well, you don’t need the first call to ToCharArray() to start with – string implements IEnumerable<char>. However, I agree that a StringBuilder and a loop would probably be more appropriate in this case.

    I’m not sure what string.Concat(char[]) does offhand, btw – why aren’t you just using the string constructor which takes a char array? In other words, after these modifications:

    static string SanitizeXml(string xml) {     return new string (xml.Where(c => IsLegalXmlChar(c)).ToArray()); } 

    I still prefer a StringBuilder solution, but that could be improved for the common case (where there are few illegal characters) by giving an appropriate capacity to start with:

    string SanitizeXml(string xml) {     var buffer = new StringBuilder(xml.Length);      foreach(char c in xml)     {         if (IsLegalXmlChar(c))         {                 buffer.Append(c);         }     }      return buffer.ToString(); } 

    One alternative I hadn’t thought of before might be an extension method on StringBuilder:

    // Can't just call it Append as otherwise StringBuilder.Append(object) would // be used :( public static StringBuilder AppendSequence(this StringBuilder builder,                                            IEnumerable<char> sequence) {     foreach (char c in sequence)     {         builder.Append(c);     }     return builder; } 

    Then you could use it like this:

    xml = new StringBuilder(xml.Length)             .AppendSequence(xml.Where(IsLegalXmlChar)             .ToString(); 

    (You could have other overloads for AppendSequence to take IEnumerable etc, if you wanted.)

    EDIT: Another alternative might be to avoid calling Append so often, using instead the overload which appends a substring. You could then again build an extension method for StringBuilder, something like (completely untested, I’m afraid – I haven’t even tried compiling it):

    public static StringBuilder AppendWhere(this StringBuilder builder,                                         string text,                                         Func<char, bool> predicate) {     int start = 0;     bool lastResult = false;     for (int i=0; i < text.Length; i++)     {         if (predicate(text[i]))         {             if (!lastResult)             {                 start = i;                 lastResult = true;             }         }         else         {             if (lastResult)             {                 builder.Append(text, start, i-start);                 lastResult = false;             }         }     }     if (lastResult)     {          builder.Append(text, start, text.Length-start);     }     return builder; } 

    Usage for the example:

    xml = new StringBuilder(xml.Length).AppendWhere(xml, IsLegalXmlChar)                                    .ToString(); 

    Another alternative would be to change it to be an extension method on String, create the StringBuilder lazily, and if you get to the end with start=0, just return the original string.

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

Sidebar

Related Questions

No related questions found

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.