2017-10-13 142 views
-4

我已經耗盡。需要幫助什麼是字符串類型適當擴展方法「改爲」 ...... words.Text ==「藍色」產生錯誤C# - 擴展方法

string userInput = textBox1.Text; 
      string[] words = userInput.Split(); 
      if (words.Text ==" blue ") 
      { 
      string color = words[2]; 
      label1.Text = "The third word is: " + color; 
      } 
      else 
      { 
       label1.Text = "Not enough words."; 
      } 
+7

'字符串不是字符串類型 - 它是'字符串數組' – Fabio

+1

'words'是一個字符串數組,它沒有'Text'屬性。你需要與包含'string'的單詞[index]進行比較。 –

+1

此外,你似乎已經知道,因爲你稍後分配顏色... – Icepickle

回答

0

你不能得到「文本」字符串數組,這個「話」是。 但是你可以這樣做:

 for (int i = 0; i < words.Length; i++) 
     { 
      Console.WriteLine(string.Format("The {0} word is {1}", i+1, words[i])); 
     } 

不過,我不excatly知道你正在嘗試做的。

0
從數組值的

正確檢索:

if (words[2] ==" blue ") 
{ 
string color = words[2]; 
label1.Text = "The third word is: " + color; 
} 
else 
{ 
    label1.Text = "Not enough words."; 
} 
0

如果您:

string userInput = textBox1.Text; 
string[] words = userInput.Split(); 

foreach(string word in words) 
{ 
    if(word == "blue") 
    { 
     string color = word; 
     label1.text = "The third word is: " + color; 
    } 
    else 
    { 
     label1.Text = "Not enough words."; 
    } 
} 

IM在什麼youre試圖做的,但如果我跟着它嚴格,你可能只是這樣做不知道'指的是Linq然後找到它可能是陣列中的藍色字

if (words.Any(x=>string.Equals(x, "blue"))) {