Bhawna Jain
Bhawna Jain

Reputation: 759

Remove character and space from a string

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.

Upvotes: 0

Views: 1342

Answers (2)

TheGeneral
TheGeneral

Reputation: 81493

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)}]".

Upvotes: 3

SᴇM
SᴇM

Reputation: 7213

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

Upvotes: 1

Related Questions