我有这个字符串: " Mimi loves Toto and Tata hate Mimi so Toto killed Tata"
我想编写一个代码,只打印以大写字母开头的单词,避免重复
输出应该是
Mimi Toto Tata
我试图这样做,但即使没有出现任何错误,我也确定错了.
我写的代码:
static void Main(string[] args) { string s = "Memi ate Toto and she killed Tata Memi also hate Biso"; Console.WriteLine((spliter(s))); } public static string spliter(string s) { string x = s; Regex exp = new Regex(@"[A-Z]"); MatchCollection M = exp.Matches(s); foreach (Match t in M) { while (x != null) { x = t.Value; } } return x; } } }
如果我将字符串拆分成数组,然后应用正则表达式逐字检查它然后打印结果怎么办?我不知道 - 任何人都可以帮我制作这段代码吗?
我根本不知道C#/ .net正则表达式lib,但这个正则表达式模式将会这样做:
\b[A-Z][a-z]+
\ b表示匹配只能从单词的开头开始.如果你想允许单字大写,请将+更改为*.
编辑:你想匹配"麦当劳"?
\b[A-Z][A-Za-z']+
如果您不想匹配'如果它只出现在字符串的末尾,那么只需执行以下操作:
\b[A-Z][A-Za-z']+(?
2> BFree..:我不知道为什么我要张贴这个......
string[] foo = "Mimi loves Toto and Tata hate Mimi so Toto killed Tata".Split(' '); HashSetwords = new HashSet (); foreach (string word in foo) { if (char.IsUpper(word[0])) { words.Add(word); } } foreach (string word in words) { Console.WriteLine(word); }
3> Michael Buen..:C#3
string z = "Mimi loves Toto and Tata hate Mimi so Toto killed Tata"; var wordsWithCapital = z.Split(' ').Where(word => char.IsUpper(word[0])).Distinct(); MessageBox.Show( string.Join(", ", wordsWithCapital.ToArray()) );C#2
DictionarydistinctWords = new Dictionary (); string[] wordsWithInitCaps = z.Split(' '); foreach (string wordX in wordsWithInitCaps) if (char.IsUpper(wordX[0])) if (!distinctWords.ContainsKey(wordX)) distinctWords[wordX] = 1; else ++distinctWords[wordX]; foreach(string k in distinctWords.Keys) MessageBox.Show(k + ": " + distinctWords[k].ToString());