更换用坏字符的字符串的坏字符 [英] Replacing bad characters of a String with bad characters
问题描述
我只是想知道什么是更换必须随后被替换字符串中的字符的最简单方法。
例如:
VAR海峡=[世界您好];
//附上所有出现的[和]用括号[]
海峡= str.Replace([,[])更换(],[]);
- 期望的结果:
[]的Hello World []]
- 在实际的结果是:
[[[]]的Hello World []]
究其原因显然是第二个替换已修改的字符串。
那么如何更换坏字与字所有出现包含坏字?
所有这些方法的快速测量显示,该 StringBuilder的是最有效的方式。
190KB文件(全部以毫秒为单位)
regexTime 40.5065 replaceTime 20.8891 stringBuilderTime 6.9776
7MB文件
regexTime 1209.3529 replaceTime 403.3985 stringBuilderTime 175.2583
顺便说一句,从直接的StringBuilder
办法的约翰·的是快一倍的的总结从 Sehe 。
我做了一个延伸出来的:
公共静态字符串EncloseChars(此字符串输入和char [] charsToEnclose,字符串leftSide,字符串rightSide){
如果(charsToEnclose == NULL || leftSide == NULL || rightSide == NULL)
抛出新的ArgumentException(为EncloseChars参数无效,charsToEnclose == NULLcharsToEnclose:leftSide == NULLleftSide:??rightSide);
的Array.Sort(charsToEnclose);
StringBuilder的SB =新的StringBuilder();
的foreach(在输入字符C){
如果(Array.BinarySearch(charsToEnclose,三)及-1)〜
sb.Append(leftSide).Append(C).Append(rightSide);
其他
sb.Append(C);
}
返回sb.ToString();
}
[世界您好]EncloseChars(新的char [] {'[',']'},[,])。
下面是一个非常的酷的方式做到这一点。但它有被pretty的优势接近万无一失,我想,而不是使用正则表达式(如果你不想使用正则表达式)。
StringBuilder的SB =新的StringBuilder();
的foreach(在str.ToCharArray()字符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屋!