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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:34:25+00:00 2026-05-22T17:34:25+00:00

I need to do something like this dreamed .trReplace : str = str.trReplace(áéíüñ,aeiu&); It

  • 0

I need to do something like this dreamed .trReplace:

  str = str.trReplace("áéíüñ","aeiu&");

It should change this string:

  a stríng with inválid charactérs

to:

  a string with invalid characters

My current ideas are:

 str = str.Replace("á","a").Replace("é","e").Replace("í","ï"...

and:

 sb = new StringBuilder(str)
 sb.Replace("á","a").
 sb.Replace("é","e")
 sb.Replace("í","ï"...

But I don’t think they are efficient for long strings.

  • 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-22T17:34:25+00:00Added an answer on May 22, 2026 at 5:34 pm

    Richard has a good answer, but performance may suffer slightly on longer strings (about 25% slower than straight string replace as shown in question). I felt complelled to look in to this a little further. There are actually several good related answers already on StackOverflow as captured below:

    Fastest way to remove chars from string

    C# Stripping / converting one or more characters

    There is also a good article on the CodeProject covering the different options.

    http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx

    To explain why the function provided in Richards answer gets slower with longer strings is due to the fact that the replacements are happening one character at a time; thus if you have large sequences of non-mapped characters, you are wasting extra cycles while re-appending together the string . As such, if you want to take a few points from the CodePlex Article you end up with a slightly modified version of Richards answer that looks like:

    private static readonly Char[] ReplacementChars = new[] { 'á', 'é', 'í', 'ü', 'ñ' };
    private static readonly Dictionary<Char, Char> ReplacementMappings = new Dictionary<Char, Char>
                                                                   {
                                                                     { 'á', 'a'},
                                                                     { 'é', 'e'},
                                                                     { 'í', 'i'},
                                                                     { 'ü', 'u'},
                                                                     { 'ñ', '&'}
                                                                   };
    
    private static string Translate(String source)
    {
      var startIndex = 0;
      var currentIndex = 0;
      var result = new StringBuilder(source.Length);
    
      while ((currentIndex = source.IndexOfAny(ReplacementChars, startIndex)) != -1)
      {
        result.Append(source.Substring(startIndex, currentIndex - startIndex));
        result.Append(ReplacementMappings[source[currentIndex]]);
    
        startIndex = currentIndex + 1;
      }
    
      if (startIndex == 0)
        return source;
    
      result.Append(source.Substring(startIndex));
    
      return result.ToString();
    }
    

    NOTE Not all edge cases have been tested.

    NOTE Could replace ReplacementChars with ReplacementMappings.Keys.ToArray() for a slight cost.

    Assuming that NOT every character is a replacement char, then this will actually run slightly faster than straigt string replacements (again about 20%).

    That being said, remember when considering performance cost, what we are actually talking about… in this case… the difference between the optimized solution and original solution is about 1 second over 100,000 iterations on a 1,000 character string.

    Either way, just wanted to add some information to the answers for this question.

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

Sidebar

Related Questions

I need something like this in TSQL string myString = 123; for (int i
I need something like this: public string GetName<Type>(Expression<Somestuffhere>) // will bet GetName<Foo>(o => o.Do())
I need something like this.. String params and Context in the same time.. protected
I need something like this: class MyClass(a: String = , b: String = )
I need something like this http://jonraasch.com/blog/a-simple-jquery-slideshow but w/o the absolute positioning. Is it possible?
Ok, I need something like this: datediff(second, date_one, date_two) < 1 dates are stored
What I need is something like this: /<[\w\d]+ ([\w\d]+\=[w\d])+\/>/ Something that would match several
I need to do something like this: <input type=button value=click id=mybtn onclick=myfunction('/myController/myAction', 'myfuncionOnOK('/myController2/myAction2', 'myParameter2');',
I guess this is a simple question. I need to do something like this:
i want something like this the user enter a website link i need check

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.