2017-02-20 101 views
0
// 5. Display the received message: 

      Console.WriteLine("[Server] Message of client recieved"); 
      string vowels = "aeiouAEIOU"; 
      for (int i = 0; i < totalBytes; i++) 

      { 
       aChar = Convert.ToChar(incomingDataBuffer[i]); 
       Console.Write(aChar); 
       if ("aeiouAEIOU".Contains(aChar)) vowels += aChar; 
       Console.Write("[Server] Vowel characters deteced: " + vowels); 
      } 

無法弄清楚爲什麼我總是得到一旦編譯出錯'string.Contains(string)'的最佳重載方法匹配有一些無效參數和參數'1':無法從'char'轉換爲'string'

回答

2

string.Contains()想要一個字符串,並且你給它一個字符。

試試這個:

if ("aeiouAEUIO".Contains(aChar.ToString())) 
    vowels += aChar; 
0

您可以將字符串轉換成char []由ToCharArray比使用包含方法。 你可以嘗試這樣的LINQ:

using System; 
using System.Linq; 

public class Program 
{ 
    public static void Main() 
    { 
     var input = "Hello world".ToCharArray(); 
     var vowels = "aeiouAEIOU".ToCharArray(); 

     input.Where(c=>vowels.Contains(c)).ToList().ForEach(v=>Console.WriteLine("[Server] Vowel characters deteced: " + v)); 
    } 
} 

.netFilddle

相關問題