0

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:

enter image description here

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.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Bhawna Jain
  • 709
  • 10
  • 27
  • Could you please clarify what should be result for `"X a i Z"` input? So far I thing regular [Remove characters](https://stackoverflow.com/questions/7411438/remove-characters-from-c-sharp-string) is good duplicate, also depending on the desired output (i.e. if you want `"X Z"` as result) then it is different question. – Alexei Levenkov Jan 31 '19 at 06:03
  • That's unfortunate. I think 10+ answers to that question cover pretty much all sensible ways to remove characters from a string... Couple of them already provided here as answers (but now it is clear neither of current answers would work as you've already tried them). I'd recommend editing post to show all variants you've tried (obviously "I don't know how to convert character to string" which is your last edit shows is not a good reason to reject the suggestion but rather potential reason for downvote due to lack of demonstrated research) – Alexei Levenkov Feb 01 '19 at 06:36
  • @AlexeiLevenkov , I have updated the question to be more clear about the posts I have referred to. Also it is important for the one who answers to have prior knowledge about the question itself (like you who doesnt know how to convert character to string)..if not then they would also end up looking here for the answers..and hence the solution will be limited. – Bhawna Jain Feb 01 '19 at 09:13
  • Bhawna - I've updated post with variants you've tried and added explanations they did not work. Feel free to improve (or just revert) if you wanted to. (Thanks for pointing out my lack of knowledge about converting characters to strings... I'll read about it somewhere) – Alexei Levenkov Feb 04 '19 at 07:07

2 Answers2

3

Assuming you want to exclude chars in a string, and replace multiple white spaces with a single space afterwards, you can use regex easily in 2 steps

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);

Output

Ths s  tst strng whch cld b ny txt
Ths s tst strng whch cld b ny txt

Full Demo Here

Note: if your string can contain characters that need to be escaped in regex use Regex.Escape as shown in following answer - $"[{Regex.Escape(exclude)}]".

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

In your situation use StringBuilder, to build your result from 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();

Edit:

In order to remove spaces from string you can do:

string result = string.Join(" ", sb.ToString().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

Output:

Ths s tst strng whch cld b ny txt

Also, here is LINQ solution for that:

var result = string.Concat(s2.Where(c => !s1.Contains(c)));

Also for this one, if you want to remove spaces in between words (you can create an extension method for that):

var raw = string.Concat(s2.Where(c => !s1.Contains(c)));
var result = string.Join(" ", raw.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

References: Enumerable.Where Method, String.Contains Method, String.Concat Method

SᴇM
  • 7,024
  • 3
  • 24
  • 41
  • This leaves extra space between s and tst, hence cant be marked as solution : "Ths s tst strng whch cld b ny txt" – Bhawna Jain Feb 01 '19 at 06:08