错误无法将类型'string []'隐式转换为'string'

时间:2015-05-21 02:27:00

标签: c#

我在每次运行程序时遇到名词[i] = SearchKeywords(单词,句子); 中的错误。我该怎么办?

*我编辑过包含SearchKeywords方法。我试图在他们面前找到所有带有“a”和“an”的单词。

错误是, 错误无法将类型'string []'隐式转换为'string'

    string[] SearchKeywords(List<string> keywords, string[] sentence){

    int[] location = new int[sentence.Length];

    foreach (string word in keywords)
    {
        for (int i = 0; i < sentence.Length;i++ )
        {
            string[] nouns = new string[i];
            if (String.IsNullOrEmpty(sentence[i]))
            {
                return null;
            }
            else
            {
                location[i] = sentence[i].IndexOf(word);

                nouns[i] = sentence[i].Substring(location[i]);

                if (nouns[i].Contains(word)) return nouns;
            }
        }

    }
    return sentence;

   string[] checkForIndefinite()
   {
       string input = txtEntry.Text;

       string text = lbltxtOutput.Text;

       Tools tool = new Tools();    

       List<string> words = new List<string>();
       words.Add("an");
       words.Add("a");

       foreach(string sentence in GetWords(text))
       {
           for (int i = 0; i < sentence.Length; i++)
           {
               string[] nouns = new string[i];
               string[] sentences = new string[i];
               sentences[i] = sentence;

               **nouns[i] = SearchKeywords(words, sentences);**

              return nouns;

           }
       }
       return null;
   }

3 个答案:

答案 0 :(得分:2)

在我看来,这就是你所需要的:

string[] SearchKeywords(List<string> keywords, string[] sentence)
{
    return
        sentence
            .Zip(sentence.Skip(1), (w0, w1) => new { w0, w1 })
            .Where(ww => keywords.Contains(ww.w0))
            .Select(ww => ww.w1)
            .ToArray();
}

string[] checkForIndefinite()
{
    var input = txtEntry.Text;
    var text = lbltxtOutput.Text;
    var words = new List<string>() { "an", "a" };
    return SearchKeywords(words, GetWords(text));
}

答案 1 :(得分:0)

SearchKeywords函数的输出为string[],而nouns[i]string,您无法将string[]值分配给string 1}}。所以你可以尝试这样的事情:

List<string> nouns = new List<string>();

//foreach loop
    //    for loop

        nouns.AddRange(SearchKeywords(words, sentences));

答案 2 :(得分:-1)

using System;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string[] indefinites = checkForIndefinite();
        foreach (string s in indefinites)
        {
            Console.WriteLine(s);
        }
    }


    static string[] SearchKeywords(List<string> keywords, string sentence)
    {
        if (String.IsNullOrEmpty(sentence))
        {
            return null;
        }

        List<string> nouns = new List<string>();

        string[] words = sentence.Split(' ');

        foreach (string keyword in keywords)
        {
            for (int i = 0; i < words.Length; i++ )
            {
                if (words[i] == keyword)
                {
                    if (i+1 < words.Length && !nouns.Contains(words[i+1]))
                    {
                        nouns.Add(words[i+1]);
                    }
                }
            }
        }
        return nouns.ToArray();
    }

   static string[] checkForIndefinite()
   {
       string sentence = "This is not an interesting sentence to use to test a program";

       string text = string.Empty;   

       List<string> words = new List<string>();
       words.Add("an");
       words.Add("a");

       return SearchKeywords(words, sentence);

   }
}

这会编译并返回以下内容:

interesting
program