Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从c#中用户输入的字符串中筛选出字符?_C#_Trim - Fatal编程技术网

如何从c#中用户输入的字符串中筛选出字符?

如何从c#中用户输入的字符串中筛选出字符?,c#,trim,C#,Trim,如何从输入的字符串中筛选出特定字符? 请看下面我是如何尝试的 using System; namespace PlainTest { class arrayTest { static void Main(string[] args) { bool doAlways = true; int i = 1;

如何从输入的字符串中筛选出特定字符? 请看下面我是如何尝试的

    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("Ater trimed the alpha a,b,c : {0}", filterAlphabet);


                } while (doAlways == true);
            }
        }
    }
但是当我尝试在数字之间修剪角色时。过滤器坏了。关于不同输入的输出,请参见下文

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

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

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

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

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

Test Number : 6
Key in the string: 
请告诉我。
谢谢。

您可以在字符串中循环查找要删除的字符,并将其替换为空字符串,而不是使用
trim

var alpha = new string[] { "a", "b", "c" };
foreach (var c in alpha)
{
    text = text.Replace(c, string.Empty);
}

您可以在字符串中循环查找要删除的字符并用空字符串替换,而不是使用
trim

var alpha = new string[] { "a", "b", "c" };
foreach (var c in alpha)
{
    text = text.Replace(c, string.Empty);
}
Trim(char[])只删除前导字符或尾随字符,与Trim()删除前导/尾随空格的方式相同。一旦Trim击中了不在数组中的角色,它就会停止(从前面和后面工作)。要从任何位置删除所需的字符,需要使用Replace或Regex。

Trim(char[])只删除前导字符或尾随字符,就像Trim()删除前导/尾随空格一样。一旦Trim击中了不在数组中的角色,它就会停止(从前面和后面工作)。要从任何地方删除所需的字符,您需要使用Replace或Regex。

您可以使用Regex

而不是

string filterAlphabet = text.Trim(alpha);
使用正则表达式替换a、b、c

string filterAlphabet = Regex.Replace(text,"[abc]",string.Empty);
你可以使用正则表达式

而不是

string filterAlphabet = text.Trim(alpha);
使用正则表达式替换a、b、c

string filterAlphabet = Regex.Replace(text,"[abc]",string.Empty);

您是否阅读了for
字符串.Trim
?从当前字符串对象中删除数组中指定的一组字符的所有前导和尾随匹配项。粗略的谷歌搜索将显示此文档和有关此问题的其他参考资料。在问下一个问题之前,请做更多的研究。您是否阅读了for
字符串.Trim
?从当前字符串对象中删除数组中指定的一组字符的所有前导和尾随匹配项。粗略的谷歌搜索将显示此文档和有关此问题的其他参考资料。在问下一个问题之前,请做更多的研究@达维贝克你是对的,我已经更新了。@达维贝克你是对的,我已经更新了。