Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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中出现的字母#_C#_.net_String - Fatal编程技术网

C# 获取字符串c中出现的字母#

C# 获取字符串c中出现的字母#,c#,.net,string,C#,.net,String,我有一根看起来像的绳子 E-1、E-2、F-3、F-1、G-1、E-2、F-5 现在,我想要像这样的数组中的输出 E、 F,G 我只希望字符串中出现一次字符的名称 我的代码示例如下 string str1 = "E-1,E-2,F-3,F-1,G-1,E-2,F-5"; string[] newtmpSTR = str1.Split(new char[] { ',' }); Dictionary<string, string> t

我有一根看起来像的绳子

E-1、E-2、F-3、F-1、G-1、E-2、F-5

现在,我想要像这样的数组中的输出

E、 F,G

我只希望字符串中出现一次字符的名称

我的代码示例如下

   string str1 = "E-1,E-2,F-3,F-1,G-1,E-2,F-5";
            string[] newtmpSTR = str1.Split(new char[] { ',' });
            Dictionary<string, string> tmpDict = new Dictionary<string, string>();
            foreach(string str in newtmpSTR){
                string[] tmpCharPart = str.Split('-');
                if(!tmpDict.ContainsKey(tmpCharPart[0])){
                    tmpDict.Add(tmpCharPart[0], "");
                }

            }
string str1=“E-1、E-2、F-3、F-1、G-1、E-2、F-5”;
字符串[]newtmpSTR=str1.Split(新字符[]{',});
字典tmpDict=新字典();
foreach(newtmpSTR中的字符串str){
字符串[]tmpCharPart=str.Split('-');
如果(!tmpDict.ContainsKey(tmpCharPart[0])){
tmpDict.Add(tmpCharPart[0],“”);
}
}
在c#中有没有简单的方法,使用string函数,如果有,怎么做

string input = "E-1,E-2,F-3,F-1,G-1,E-2,F-5";
string[] splitted = input.Split(new char[] { ',' });
var letters = splitted.Select(s => s.Substring(0, 1)).Distinct().ToList();

也许您可以使用正则表达式获得相同的结果!:-)

Liam你能告诉那些只访问这个问题来否决投票的人吗?你的反馈真的很重要。人们否决这个问题是因为他们缺乏努力。你通过显示一些代码来改进它,但是你原来的问题有点像我自己解决不了的,请有人帮我解决。查看页面可能是一个好主意Hanks Liam会记住这一点不要问太多不受欢迎的问题。你可以。在发帖之前,一定要考虑你的问题。仅供参考,删除不好的问题不会改变你被禁止的机会。最好先问个好问题