用坏字符替换字符串的坏字符 [英] Replacing bad characters of a String with bad characters

查看:21
本文介绍了用坏字符替换字符串的坏字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道替换必须随后替换的字符串字符的最简单方法是什么.

例如:

var str = "[Hello World]";//用方括号[]将所有出现的[和]括起来str = str.Replace("[","[[]").Replace("]","[]]");

  • 想要的结果:[[]Hello World[]]
  • 实际结果:[[[]]Hello World[]]

原因显然是对已经修改的字符串进行了第二次替换.

那么如何用包含坏"字符的字符替换所有出现的坏"字符?

<小时>

对所有方法的快速测量表明,StringBuilder 是最有效的方法.

190kb 文件(以毫秒为单位)

<块引用>

 regexTime 40.5065更换时间 20.8891字符串生成器时间 6.9776

7MB 文件

<块引用>

 regexTime 1209.3529替换时间 403.3985字符串生成器时间 175.2583

顺便说一下,John 的直接 StringBuilder 方法比 来自Sehe的聚合方法.

我已经对其进行了扩展:

public static String EncloseChars(this string input, char[] charsToEnclose, String leftSide, String rightSide) {if (charsToEnclose == null || leftSide == null || rightSide == null)throw new ArgumentException("EncloseChars 的参数无效", charsToEnclose == null ? "charsToEnclose" : leftSide == null ? "leftSide" : "rightSide");Array.Sort(charsToEnclose);StringBuilder sb = new StringBuilder();foreach(输入中的字符 c){if (Array.BinarySearch(charsToEnclose, c) > -1)sb.Append(leftSide).Append(c).Append(rightSide);别的sb.Append(c);}返回 sb.ToString();}"[Hello World]".EncloseChars(new char[]{'[', ']'},"[","]");

解决方案

这是一种非常不酷的方法.但我认为它的优点是非常接近万无一失,并且不使用正则表达式(以防您不想使用正则表达式).

StringBuilder sb = new StringBuilder();foreach (str.ToCharArray() 中的字符 c) {if (c == '[' || c == ']') {sb.Append('[' + c + ']');}别的 {sb.Append(c);}}字符串结果 = sb.ToString();

I just wondered what's the easiest way to replace a string characters that must be replaced subsequently.

For example:

var str = "[Hello World]";
//enclose all occurences of [ and ] with brackets[] 
str = str.Replace("[","[[]").Replace("]","[]]");

  • The desired result: [[]Hello World[]]
  • The actual result: [[[]]Hello World[]]

The reason is obviously the second replace on the already modified string.

So how to replace all occurences of "bad" characters with characters that contain "bad" characters?


A quick measurement of all approaches revealed that the StringBuilder is the most efficient way.

190kb file (all in milliseconds)

  regexTime           40.5065  
  replaceTime         20.8891  
  stringBuilderTime    6.9776

7MB file

  regexTime           1209.3529           
  replaceTime          403.3985   
  stringBuilderTime    175.2583

By the way, the direct StringBuilder approach from John was twice as fast as the Aggregate approach from Sehe.

I've made an extension out of it:

public static String EncloseChars(this string input, char[] charsToEnclose, String leftSide, String rightSide) {
    if (charsToEnclose == null || leftSide == null || rightSide == null)
        throw new ArgumentException("Invalid arguments for EncloseChars", charsToEnclose == null ? "charsToEnclose" : leftSide == null ? "leftSide" : "rightSide");
    Array.Sort(charsToEnclose);
    StringBuilder sb = new StringBuilder();
    foreach (char c in input) {
        if (Array.BinarySearch(charsToEnclose, c) > -1)
            sb.Append(leftSide).Append(c).Append(rightSide);
        else 
            sb.Append(c);
    }
    return sb.ToString();
}

"[Hello World]".EncloseChars(new char[]{'[', ']'},"[","]");

解决方案

Here's a very uncool way to do it. But it has the advantage of being pretty close to foolproof, I think, and not using regex (in case you'd rather not use regex).

StringBuilder sb = new StringBuilder();
foreach (char c in str.ToCharArray()) {
    if (c == '[' || c == ']') {
        sb.Append('[' + c + ']');
    }
    else {
        sb.Append(c);
    }
}
string result = sb.ToString();

这篇关于用坏字符替换字符串的坏字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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