找到已知单词

时间:2015-07-14 20:43:46

标签: c# arrays char

我有一个我在C#工作的项目。我有两个char数组。一个是句子,一个是单词。我必须遍历句子数组,直到找到一个与转换成单词数组的单词匹配的单词,我想知道的是一旦我找到单词,我如何向后遍历句子数组在这一点上,我发现这个词与单词数组的长度相同?

代码:

String wordString = "(Four)";
String sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
char[] wordArray = wordString.ToCharArray();
List<String> words = sentenceString.Split(' ').ToList<string>();
//This would be the part where I iterate through sentence
foreach (string sentence in sentArray)
{
     //Here I would need to find where the string of (Four) and trim it and see if it equals the wordString. 
     if (sentence.Contains(wordString)
     {
         //At this point I would need to go back the length of wordString which happens to be four places but I'm not sure how to do this.  And for each word I go back in the sentence I need to capture that in another string array.
      }

我不知道我是否对此表现得足够清楚,但如果我不是,请随意提问..先谢谢你。此外应该归还的是乌拉圭中继者的家禽&#34;。所以基本上用例是括号中的字母数,逻辑应该在括号中的单词之前返回相同数量的单词。

9 个答案:

答案 0 :(得分:2)

int i ;
string outputString = (i=sentenceString.IndexOf(wordString))<0 ?     
                          sentenceString : sentenceString.Substring(0,i) ;

答案 1 :(得分:2)

var wordString = "(Four)";
            int wordStringInt = 4; // Just do switch case to convert your string to int
            var sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
            var sentenceStringArray = sentenceString.Split(' ').ToList();
            int wordStringIndexInArray = sentenceStringArray.IndexOf(wordString) - 1;
            var stringOutPut = "";
            if (wordStringIndexInArray > 0 && wordStringIndexInArray > wordStringInt)
            {
                stringOutPut = "";
                while (wordStringInt > 0)
                {
                    stringOutPut = sentenceStringArray[wordStringInt] + " " + stringOutPut;
                    wordStringInt--;
                }

            }

答案 2 :(得分:2)

我们是你。关于这个练习我几乎没有问题。 如果Word (四)刚开始它不应该返回?或者返回所有字符串? 因为长度为4等于4 想象一下,如果该单词出现在句子的第二个单词上,它应该只返回第一个单词还是返回4个单词,甚至包括(四个)单词。

我的解决方案是最懒的,我只是看到你的问题并决定提供帮助。

  • 如果长度大于(四个)字之前的字词,我的解决方案假设它返回(四个)之前的所有字词。
  • 如果(四)这个词在开头,我的解决方案会返回空字符串。
  • 我的解决方案在(四个)字之前返回(四个)(4)字的长度。

再一次,这不是我最好的方法。

我看到下面的代码:

        string wordString = "(Four)";
        string sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
        //Additionally you can add splitoption to remove the empty word on split function bellow
        //Just in case there are more space in sentence.
        string[] splitedword = sentenceString.Split(' ');
        int tempBackposition = 0;
        int finalposition = 0;
        for (int i = 0; i < splitedword.Length; i++)
        {
            if (splitedword[i].Contains(wordString))
            {
                finalposition = i;
                break;
            }
        }
        tempBackposition = finalposition - wordString.Replace("(","").Replace(")","").Length;
        string output = "";
        tempBackposition= tempBackposition<0?0:tempBackposition;
        for (int i = tempBackposition; i < finalposition; i++)
        {
            output += splitedword[i] + " ";
        }
        Console.WriteLine(output);
        Console.ReadLine();

如果不是您想要的,您可以回答我的问题吗?还是帮我理解这是不对的

答案 3 :(得分:2)

您所匹配的内容有点复杂,因此对于更通用的解决方案,您可以使用正则表达式。

首先我们宣布我们要搜索的内容:

string word = "(Four)";
string sentence = "Maybe the fowl of Uruguay repeaters (Four) will be found";

然后我们将使用正则表达式搜索此字符串中的单词。由于我们不想匹配空格,我们需要知道每个匹配实际开始的位置我们需要知道括号内的单词,我们告诉它我们可选择要打开和结束括号,但我们也希望这些内容匹配:

var words = Regex.Matches(sentence, @"[\p{Ps}]*(?<Content>[\w]+)[\p{Pe}]*").Cast<Match>().ToList();

[\p{Ps}]表示我们需要打开标点符号([{等,而*表示零或更多。

Followed是一个名为Content(由?<Content>指定)的子捕获,带有一个或多个单词字符。 最后,我们指定我们想要零或更多结束标点符号。

然后我们需要在匹配列表中找到该词:

var item = words.Single(x => x.Value == word);

然后我们需要找到这个项目的索引:

int index = words.IndexOf(item);

此时我们只需要知道内容的长度:

var length = item.Groups["Content"].Length;

这个长度我们用来回到字符串4个单词

var start = words.Skip(index - length).First();

现在我们拥有了所需的一切:

var result = sentence.Substring(start.Index, item.Index - start.Index);

结果应包含fowl of Uruguay repeaters

编辑:从单词而不是内容中找出计数可能要简单得多。在这种情况下,完整的代码应如下:

string word = "(Four)";
string sentence = "Maybe the fowl of Uruguay repeaters (Four) will be found";

var wordMatch = Regex.Match(word, @"[\p{Ps}]*(?<Content>[\w]+)[\p{Pe}]*");
var length = wordMatch.Groups["Content"].Length;

var words = Regex.Matches(sentence, @"\S+").Cast<Match>().ToList();
var item = words.Single(x => x.Value == word);
int index = words.IndexOf(item);


var start = words.Skip(index - length).First();
var result = sentence.Substring(start.Index, item.Index - start.Index);
在这种情况下,

\S+表示&#34;匹配一个或多个非空白字符&#34;。

答案 4 :(得分:2)

您应该尝试以下内容,在找到数字后使用Array.Copy。您仍然必须正确实现ConvertToNum函数(现在它已经硬编码),但这应该是一个快速简单的解决方案。

string[] GetWords()
{
    string sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
    string[] words = sentenceString.Split();
    int num = 0;
    int i; // scope of i should remain outside the for loop
    for (i = 0; i < words.Length; i++)
    {
        string word = words[i];
        if (word.StartsWith("(") && word.EndsWith(")"))
        {
            num = ConvertToNum(word.Substring(1, word.Length - 1));
            // converted the number word we found, so we break
            break;
        }
    }
    if (num == 0)
    {
        // no number word was found in the string - return empty array
        return new string[0];
    }
        // do some extra checking if number word exceeds number of previous words
        int startIndex = i - num;
        // if it does - just start from index 0
        startIndex = startIndex < 0 ? 0 : startIndex;
        int length = i - startIndex;
        string[] output = new string[length];
        Array.Copy(words, startIndex, output, 0, length);
        return output;
}


// Convert the number word to an integer
int ConvertToNum(string numberStr)
{
    return 4; // you should implement this method correctly
}

有关实施ConvertToNum解决方案的帮助,请参阅 - Convert words (string) to Int。显然,它可以根据您期望处理的数字范围进行简化。

答案 5 :(得分:2)

这是我的解决方案,我根本不使用正则表达式,以便于理解:

        static void Main() {
            var wordString = "(Four)";
            int wordStringLength = wordString.Replace("(","").Replace(")","").Length; 
            //4, because i'm assuming '(' and ')' doesn't count. 

            var sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";

            //Transform into a list of words, ToList() to future use of IndexOf Method
            var sentenceStringWords = sentenceString.Split(' ').ToList();

            //Find the index of the word in the list of words
            int wordIndex = sentenceStringWords.IndexOf(wordString);

            //Get a subrange from the original list of words, going back x Times the legnth of word (in this case 4),

            var wordsToConcat = sentenceStringWords.GetRange(wordIndex-wordStringLength, wordStringLength);

            //Finally concat the output;
            var outPut = string.Join(" ", wordsToConcat);

            //Output: fowl of Uruguay repeaters

        }

答案 6 :(得分:0)

我有一个解决方案:

string wordToMatch = "(Four)";
            string sentence = "Maybe the fowl of Uruguay repeaters (Four) will be found";

            if (sentence.Contains(wordToMatch))
            {
                int length = wordToMatch.Trim(new[] { '(', ')' }).Length;
                int indexOfMatchedWord = sentence.IndexOf(wordToMatch);
                string subString1 = sentence.Substring(0, indexOfMatchedWord);
                string[] words = subString1.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var reversed = words.Reverse().Take(length);

                string result = string.Join(" ", reversed.Reverse());
                Console.WriteLine(result);
                Console.ReadLine();
            }

可能这可以改善表现,但我有一种感觉你不关心这一点。确保您使用的是System.Linq&#39;

答案 7 :(得分:0)

当输入不完整时,我假设空的回报,随时纠正我。你的帖子中没有100%清楚如何处理这个问题。

    private string getPartialSentence(string sentence, string word)
    {
        if (string.IsNullOrEmpty(sentence) || string.IsNullOrEmpty(word))
            return string.Empty;

        int locationInSentence = sentence.IndexOf(word, StringComparison.Ordinal);

        if (locationInSentence == -1)
            return string.Empty;

        string partialSentence = sentence.Substring(0, locationInSentence);
        string[] words = partialSentence.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

        int nbWordsRequired = word.Replace("(", "").Replace(")", "").Length;

        if (words.Count() >= nbWordsRequired)
            return String.Join(" ", words.Skip(words.Count() - nbWordsRequired));

        return String.Join(" ", words);
    }

答案 8 :(得分:0)

我使用枚举和关联字典将“(四)”类型字符串与其整数值配对。您可以使用

轻松(也可能更容易)使用switch语句
case "(Four)": { currentNumber = 4; };

我觉得enum允许更多的灵活性。

public enum NumberVerb
    {
        one = 1,
        two = 2,
        three = 3,
        four = 4,
        five = 5,
        six = 6,
        seven = 7,
        eight = 8,
        nine = 9,
        ten = 10,
    };

    public static Dictionary<string, NumberVerb> m_Dictionary
    {
        get
        {
            Dictionary<string, NumberVerb> temp = new Dictionary<string, NumberVerb>();

            temp.Add("(one)", NumberVerb.one);
            temp.Add("(two)", NumberVerb.two);
            temp.Add("(three)", NumberVerb.three);
            temp.Add("(four)", NumberVerb.four);
            temp.Add("(five)", NumberVerb.five);
            temp.Add("(six)", NumberVerb.six);
            temp.Add("(seven)", NumberVerb.seven);
            temp.Add("(eight)", NumberVerb.eight);
            temp.Add("(nine)", NumberVerb.nine);
            temp.Add("(ten)", NumberVerb.ten);

            return temp;
        }
    }

    static void Main(string[] args)
    {
        string resultPhrase = "";

        // Get the sentance that will be searched.
        Console.WriteLine("Please enter the starting sentance:");
        Console.WriteLine("(don't forget your keyword: ie '(Four)')");
        string sentance = Console.ReadLine();

        // Get the search word.
        Console.WriteLine("Please enter the search keyword:");
        string keyword = Console.ReadLine();

        // Set the associated number of words to backwards-iterate.
        int currentNumber = -1;
        try 
        {
            currentNumber = (int)m_Dictionary[keyword.ToLower()];
        }
        catch(KeyNotFoundException ex)
        {
            Console.WriteLine("The provided keyword was not found in the dictionary.");
        }

        // Search the sentance string for the keyword, and get the starting index.
        Console.WriteLine("Searching for phrase...");

        string[] words = sentance.Split(' ');
        int searchResultIndex = -1;

        for (int i = 0; (searchResultIndex == -1 && i < words.Length); i++)
        {
            if (words[i].Equals(keyword))
            {
                searchResultIndex = i;
            }
        }

        // Handle the search results.
        if (searchResultIndex == -1)
        {
            resultPhrase = "The keyword was not found.";
        }
        else if (searchResultIndex < currentNumber)
        {
            // Check the array boundaries with the given indexes.
            resultPhrase = "Error: Out of bounds!";
        }
        else
        {
            // Get the preceding words.
            for (int i = 0; i < currentNumber; i++)
            {
                resultPhrase = string.Format(" {0}{1}", words[searchResultIndex - 1 - i], resultPhrase);
            }
        }

        // Display the preceding words.
        Console.WriteLine(resultPhrase.Trim());

        // Exit.
        Console.ReadLine();
    }