[英]Remove character and space from a string
我正在尝试从另一个字符串中删除出现在一个字符串上的所有字符。 理想情况下,生成的字符串将不会包含两个相邻的空格,至少删除的字符一定不能替换为空格(或其他任何不可见的字符)。
我想出了以下代码,但如果这样做,则会留下某种空格(除了具有多个顺序空格,而不是" a "
)。 也有删除方法,但是它需要索引,因此会使解决方案复杂化。
String s1="aeiou";
String s2="This is a test string which could be any text";
Console.WriteLine(s2);
for (int i=0; i<s1.Length; i++)
{
if(s2.Contains(s1[i]))
{
s2= s2.Replace(s1[i],'\0');
}
}
Console.WriteLine(s2);
输出:
预期产量:
这是tst strng whch cld b ny txt
我使用'\\ 0'作为string.Replace()
仅期望字符,并且第二个参数的版本为字符串。 string.Empty
第一个参数也必须为字符串(这需要转换-稍后显示为“变量1”)。
我已经从这些相关的/建议的重复文章中引用了( 从C#字符串中删除字符,从字符串c#中删除'\\'char ),但是没有找到完全令我满意的方法。
变体1 (基于投票最多的答案 。此版本需要将我要替换的每个字符转换为我不喜欢的字符串:
String s1="aeiou";
String s2="This is a test string which could be any text";
Console.WriteLine(s2);
foreach(var c in s1)
{
s2 = s2.Replace(c.ToString(), string.Empty);
}
Console.WriteLine(s2);
变体2 - String.Join
与String.Split
( answer )。 当我希望避免这种情况时,需要将源替换字符串转换为数组。
String s1="aeiou";
String s2="This is a test string which could be any text";
s2 = String.Join("", s2.Split(s1.ToCharArray()));
变体3 - Regex.Replace
( answer )-这比变体2还要复杂,因为我需要将替换字符串转换为正确的正则表达式,可能因为"^!"
类的东西而被完全破坏"^!"
作为要替换的字符串(在这种情况下也不需要):
String s1="aeiou";
String s2="This is a test string which could be any text";
s2 = Regex.Replace(s2, "["+s1+"]", String.Empty);
Console.WriteLine(s2);
变体4使用Linq从结果char数组构造字符串( 答案要求在构造字符串之前将结果序列转换为数组(理想情况下应避免):
String s1="aeiou";
String s2="This is a test string which could be any text";
s2 = new string(s2.Where(c => !s1.Contains(c)).ToArray());
Console.WriteLine(s2);
变体5-使用String.Concat
( answer )到目前为止看起来最好,但是使用Linq(我不愿意...也许没有充分的理由在这里使用Linq)
String s1="aeiou";
String s2="This is a test string which could be any text";
s2 = string.Concat(s2.Where(c => !s1.Contains(c)));
Console.WriteLine(s2);
我提出的解决方案都没有删除重复的空格 ,所有X版本都确实删除了字符,但是我的情况有一些问题。 理想的答案不会创建太多额外的字符串,不会产生Linq,也不会产生额外的数组转换。
假设您要排除字符串中的字符,然后再用单个空格替换多个空格, regex
可以分两步轻松使用regex
string input = "This is a test string which could be any text";
string exclude = "aeiou";
var stripped = Regex.Replace(input, $"[{exclude}]", ""); // exclude chars
var cleaned = Regex.Replace(stripped, "[ ]{2,}", " "); // replace multiple spaces
Console.WriteLine(stripped);
Console.WriteLine(cleaned);
产量
Ths s tst strng whch cld b ny txt
Ths s tst strng whch cld b ny txt
注意:如果您的字符串可以包含需要在正则表达式中转义的字符,请使用Regex.Escape
,如以下答案所示- $"[{Regex.Escape(exclude)}]"
。
在您的情况下,使用StringBuilder
从s2
构建结果:
String s1 = "aeiou";
String s2 = "This is a test string which could be any text";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s2.Length; i++)
{
// Check if current char is not contained in s1,
// then add it to sb
if (!s1.Contains(s2[i]))
{
sb.Append(s2[i]);
}
}
string result = sb.ToString();
编辑:
为了从字符串中删除空格,您可以执行以下操作:
string result = string.Join(" ", sb.ToString().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
输出:
这是tst strng whch cld b ny txt
另外,这是LINQ解决方案:
var result = string.Concat(s2.Where(c => !s1.Contains(c)));
同样,对于这一点,如果要删除单词之间的空格(可以为此创建扩展方法):
var raw = string.Concat(s2.Where(c => !s1.Contains(c)));
var result = string.Join(" ", raw.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
参考: Enumerable.Where方法 , String.Contains方法 , String.Concat方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.