我有一个字符串数组:
string[] arr2 = { "/", "@", "&" };
我有另一个字符串(即strValue
)。是否有一种干净的方法用单个值(即下划线)替换数组内容的所有实例?所以之前:
strValue = "a/ new string, with some@ values&"
之后:
strValue = "a_ new string, with some_ values_"
我考虑过这样做:
strValue = strValue.Replace("/", "_");
strValue = strValue.Replace("@", "_");
strValue = strValue.Replace("&", "_");
但我要替换的角色数组可能会变得更大。
答案 0 :(得分:5)
而不是一遍又一遍地使用Replace
,你可以自己编写。这可能是你提到的性能提升
但是我的阵列可能会变得更大。
public string Replace(string original, char replacement, params char[] replaceables)
{
StringBuilder builder = new StringBuilder(original.Length);
HashSet<char> replaceable = new HashSet<char>(replaceables);
foreach(Char character in original)
{
if (replaceable.Contains(character))
builder.Append(replacement);
else
builder.Append(character);
}
return builder.ToString();
}
public string Replace(string original, char replacement, string replaceables)
{
return Replace(original, replacement, replaceables.ToCharArray());
}
可以像这样调用:
Debug.WriteLine(Replace("a/ new string, with some@ values&", '_', '/', '@', '&'));
Debug.WriteLine(Replace("a/ new string, with some@ values&", '_', new[] { '/', '@', '&' }));
Debug.WriteLine(Replace("a/ new string, with some@ values&", '_', existingArray));
Debug.WriteLine(Replace("a/ new string, with some@ values&", '_',"/@&"));
输出:
a_ new string, with some_ values_
a_ new string, with some_ values_
a_ new string, with some_ values_
a_ new string, with some_ values_
正如@Sebi所指出的,这也可以作为一种扩展方法:
public static class StringExtensions
{
public static string Replace(this string original, char replacement, params char[] replaceables)
{
StringBuilder builder = new StringBuilder(original.Length);
HashSet<Char> replaceable = new HashSet<char>(replaceables);
foreach (Char character in original)
{
if (replaceable.Contains(character))
builder.Append(replacement);
else
builder.Append(character);
}
return builder.ToString();
}
public static string Replace(this string original, char replacement, string replaceables)
{
return Replace(original, replacement, replaceables.ToCharArray());
}
}
用法:
"a/ new string, with some@ values&".Replace('_', '/', '@', '&');
existingString.Replace('_', new[] { '/', '@', '&' });
// etc.
答案 1 :(得分:3)
这是我如何从分隔符列表构建一个正则表达式子句并用下划线替换它们
string[] delimiters = { "/", "@", "&" };
string clause = $"[{string.Join("]|[", delimiters)}]";
string strValue = "a/ new string, with some@ values&";
Regex chrsToReplace = new Regex(clause);
string output = chrsToReplace.Replace(strValue, "_");
您可能希望封装在 if(delimiters.Any())中,否则如果数组为空则会崩溃
答案 2 :(得分:2)
不确定。这是一种方法:
var newString = arr2.Aggregate(strValue, (net, curr) => net.Replace(curr, "_"));
如果您只是替换单个字符并且具有足够大的输入大小以进行优化,则可以创建一个替换的集合:
var substitutions = new HashSet<char>() { '/', '@', '&' };
var strValue = "a/ new string, with some@ values&";
var newString = new string(strValue.Select(c => substitutions.Contains(c) ? '_' : c).ToArray());
答案 3 :(得分:0)
您可以使用字符串[]列表分割字符串:
string[] arr2 = { "/", "@", "&" };
string strValue = "a/ new string, with some@ values&";
string Output = null;
string[] split = strValue.Split(arr2, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in split)
{
Output += item + "_";
}
Console.WriteLine(Output);
//-> a_ new string, with some_ values_
<小时/> 更新了@aloisdg评论的答案(有趣的文章,谢谢)。
string[] arr2 = { "/", "@", "&" };
string strValue = "a/ new string, with some@ values&";
string[] split = strValue.Split(arr2, StringSplitOptions.RemoveEmptyEntries);
StringBuilder Output = new StringBuilder();
foreach (var item in split)
{
Output.Append(item + "_");
}
Console.WriteLine(Output);
//-> a_ new string, with some_ values_
答案 4 :(得分:0)
也许不是最快但最简单的是Select
Contains
。
这样的事情:source.Select(c => blacklist.Contains(c) ? letter : c)
.NetFiddle上的演示。
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var strValue = "a/ new string, with some@ values&";
Console.WriteLine(strValue.Replace("/@&", '_'));
}
}
public static class Extensions {
public static string Replace(this string source, string blacklist, char letter) =>
new string(source.Select(c => blacklist.Contains(c) ? letter : c).ToArray());
}
答案 5 :(得分:0)
您可以在一行中使用foreach来实现您的目标:
arr2.ToList().ForEach(x => strValue = strValue.Replace(x, "_"));