0

How can I filter out the specific characters from an inputted string?

Please see below for how I have tried.

using System;

namespace PlainTest
{
    class arrayTest
    {
        static void Main(string[] args)
        {
            bool doAlways = true;
            int i = 1;
            do
            {
                Console.WriteLine("Test Number : {0}", i++);
                Console.Write("Key in the string: ");
                char[] alpha = { 'a', 'b', 'c' };
                string text = Console.ReadLine();
                string filterAlphabet = text.Trim(alpha);
                Console.WriteLine("The input is : {0}", text);
                Console.WriteLine("After trimmed the alpha a,b,c : {0}", filterAlphabet);
            } while (doAlways == true);
        }
    }
}

But when I tried with the character to be trimmed in between the numbers, the filter didn't work. Please see below for outputs for different inputs.

Test Number : 1
Key in the string: 123abc
The input is : 123abc
After trimmed the alpha a,b,c : 123

Test Number : 2
Key in the string: abc123
The input is : abc123
After trimmed the alpha a,b,c : 123

**Test Number : 3
Key in the string: aa1bb2cc3
The input is : aa1bb2cc3
After trimmed the alpha a,b,c : 1bb2cc3**

Test Number : 4
Key in the string: aaabbbccc123
The input is : aaabbbccc123
After trimmed the alpha a,b,c : 123

Test Number : 5
Key in the string: a12bc
The input is : a12bc
After trimmed the alpha a,b,c : 12

Test Number : 6
Key in the string:
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    Did you read the [documentation](https://msdn.microsoft.com/en-us/library/d4tt83f9(v=vs.110).aspx) for `String.Trim`? *Removes all **leading and trailing** occurrences of a set of characters specified in an array from the current String object.* A cursory Google search would show this documentation and additional references regarding this problem. Please make more research effort before asking your next question. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – tnw Jul 25 '17 at 15:12

3 Answers3

2

Instead of using trim, you can loop through the string to look for the characters you want to remove and replace them with an empty string:

var alpha = new string[] { "a", "b", "c" };
foreach (var c in alpha)
{
    text = text.Replace(c, string.Empty);
}
Robyn MacCallum
  • 206
  • 3
  • 12
0

Trim(char[]) only removes leading or trailing characters, in the same way that Trim() removes leading/trailing white space. As soon as Trim hits a character that is not in the array, it stops (working both from the front and from the back). To get rid of your desired characters from anywhere, you need to use either Replace or a regular expression.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan Willcock
  • 5,012
  • 3
  • 20
  • 31
0

You can use Regex.

Instead of

string filterAlphabet = text.Trim(alpha);

Use regex to replace a,b,c

string filterAlphabet = Regex.Replace(text,"[abc]",string.Empty);
nobody
  • 10,892
  • 8
  • 45
  • 63