其有效的版本替换无效字符的列表(如TR) [英] Replace a list of invalid character with their valid version (like tr)

查看:153
本文介绍了其有效的版本替换无效字符的列表(如TR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做这样的事情的梦想 .trReplace

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

  a string with invalid characters



字符串

我现在的想法是:

My current ideas are:

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

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

但我不认为他们是高效的为长字符串。

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

推荐答案

理查德有一个很好的答案,但性能可能在更长的字符串(如图问题约25%,比直串慢替换)略苦。我觉得complelled在看这个远一点,实际上有几个很好的相关答案已经在计算器如下捕获:

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#剥线/转换一个或多个字符

有也对CodeProject上的好文章涵盖了不同的选择

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

HTTP:// WWW .codeproject.com / KB /串/ fastestcscaseinsstringrep.aspx

要解释为什么理查兹回答提供的功能变慢较长的字符串是由于这样的事实该替换正在发生一次一个字符;因此,如果你有非映射字符大序列,你是在浪费额外的周期,同时再追加在一起的字符串。因此,如果你想从CodePlex上条带几个点你最终理查兹回答稍微修改后的版本,看起来像:

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.

注意能取代ReplacementMappings.Keys.ToArray()ReplacementChars了轻微的成本。

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

假设不是每个字符是一个字符替换,那么这将实际运行比straigt字符串替换(再次约20%)略快。

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

话虽这么说,记得考虑性能开销,我们实际上是在谈论...在这种情况下,当......的最佳解决方案与原来的解决方案之间的差异约1秒超过10万次迭代在1000字符串。

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.

这篇关于其有效的版本替换无效字符的列表(如TR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆