C# - 扩展方法

时间:2017-10-13 07:28:09

标签: c# methods

我已经筋疲力尽了。需要帮助什么是字符串类型“单词”的正确扩展方法..... words.Text ==“blue”产生错误

  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.";
            }

3 个答案:

答案 0 :(得分:0)

你无法得到" .Text"在字符串数组上,"字"是。 但是你可以这样做:

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

但是,我并不十分确定你要做什么。

答案 1 :(得分: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.";
    }
}

我不确定你要做什么,但如果我严格遵守它,你可以这样做:

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

答案 2 :(得分:0)

如果您指的是Linq,那么在数组中找到蓝色字可能是

QProcess p;

QStringList params;

params.clear();
params.append("C:\\Software\\Qt\\StarLib\\cadstar_workspace_paths.py");
params.append("C:\\Software\\Qt\\StarLib\\TP1268_RDU5.csw");
params.append("C:\\Software\\Qt\\StarLibcadstar_local_user_directories.txt");

p.setWorkingDirectory("C:\\Software\\Qt\\StarLib");
p.setProgram("py");
p.setArguments(params);

p.start();
if (!p.waitForStarted()) {
    ui->textEdit->append("Could not start process");
    return;
}
else
    ui->textEdit->append("The program seems to have started.");
QTime time;
time.start();
while (time.elapsed() < 4000)
        QApplication::processEvents();

p.kill();
p.waitForFinished();