I am trying to remove all the characters appearing on one string from another. Ideally resulting string will not contain two spaces next to each other, at very least removed characters must not be replaced with spaces (or any other invisible characters).
I come up with following code but some sort of a space is left behind if I do so (in addition to having multiple sequential spaces instead of " a "
). There is a remove method as well but it required an index and hence will be complicating the solution.
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);
Output:
Expected Output:
Ths s tst strng whch cld b ny txt
I used '\0' as string.Replace()
is expecting characters only and for version with the second argument to be string.Empty
first argument must be string too (which requires conversion - shown as "variant 1" later).
I already took reference from these related/suggested as duplicates posts (Remove characters from C# string, Remove '\' char from string c#) and did not find any approach that completely satisfy me.
Variant 1 (based on most voted answer. This version requires converting each character I want to replace to string which I don't like:
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);
Variant 2 - String.Join
with String.Split
(answer). Requires converting my source replace string into array when I'd prefer to avoid that.
String s1="aeiou";
String s2="This is a test string which could be any text";
s2 = String.Join("", s2.Split(s1.ToCharArray()));
Variant 3 - Regex.Replace
(answer) - this is even more complicated than variant 2 as I need to convert my replace string into proper regular expression, potentially being totally broken for something like "^!"
as string to replace (also not needed in this particular case):
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);
Variant 4 using Linq with constructing string from resulting char array (answer requires converting resulting sequence into array before constructing the string (which ideally should be avoided):
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);
Variant 5 - using String.Concat
(answer) which so far looks the best but using Linq (I prefer not to... also maybe there is no good reason to be concerned of using Linq here)
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);
None of the solution I come up remove duplicate spaces, all variant X version do remove characters just fine but have some issues for my case. Ideal answer will not create too many extra strings, no Linq and no extra conversions to arrays.