C# - >中继器并通过数组循环显示它们

时间:2015-02-20 03:11:18

标签: c# arrays repeater

我的问题是好奇心和部分帮助,所以请耐心等待。

我之前的问题与将文本文件作为参数传递给函数有关,我设法在帮助下弄清楚,所以感谢所有以前帮助过的人。

所以,请考虑以下代码:

protected bool FindWordInFile(StreamReader wordlist, string word_to_find) 
{
    // Read the first line.
    string line = wordlist.ReadLine();

    while (line != null)
    {
       if(line.Contains(word_to_find))
       {
           return true;
        }

       // Read the next line.
       line = wordlist.ReadLine();
     }

     return false;
 }

如果您通过以下方式调用此特定函数会发生什么:

temp_sentence_string = post_words[i]; //Takes the first string in the array FROM the array and binds it to a temporary string variable
WordCount.Text = WordCount.Text + " ||| " + temp_sentence_string;
word_count = temp_sentence_string.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); 


for (int word_pos = 0; word_pos < word_count.Length; word_pos++)
{                            
      bool WhatEver = FindWordInFile(goodwords_string, word_count[word_pos]);
      if (WhatEver == true)
      {
           WordTest.Text = WordTest.Text + "{" + WhatEver + "} ";
      }

      WordTest.Text = WordTest.Text + "{" + WhatEver + "}";
}

传递的字符串是“好时光好”,文本文件中包含“好”字样:

good{True}times{False}are{False}good{False}

很奇怪。看起来发生的事情是:  句子“美好时光好”被放入一个阵列中,通过检测空间分开。这是正确的。  2.将第一个数组元素“good”与文本文件进行比较并返回True。这样才有效。  然后它转到了下一个单词“times”,相比之下,出现了False。  4.接下来的单词“are”,相比之下,出现了False。  然后它得到了最后一句话,“好”,但它评价为假。这应该不会发生。

所以,我的问题是 - 发生了什么?看起来FindWordInFile的函数可能没有在我的末尾编码,并且不管怎样它仍然返回False,即使文本文件中有“good”这个词。

第二部分:ASP.NET和C#中的转发器

所以我有一个转发器对象绑定到一个INSIDE for for循环的数组。这个特殊的算法采用一系列句子,然后将它们分解成一个临时的单词数组。临时数组绑定到Repeater。

但是发生的事情是,让我说我有两个句子可以做......

所以它在循环中。它对第一个单词数组进行处理,然后对第二个单词数组进行处理,但是在显示数组内容时会发生什么,它只显示生成和填充的LAST数组的内容。即使它在for循环中,我的期望是它会一个接一个地显示所有单词数组。但它只显示了最后一个。因此,如果要分解5个句子,它只显示由单词填充的第5个句子。

任何想法为什么?

for (int i = 0; i < num_sentences; i++) //num_sentences is an int that counted the number of elements in the array of sentences that was populated before. It populates the array by splitting based on punctuation.
{
    temp_sentence_string = post_words[i]; //Takes the first string in the array FROM the sentence array and binds it to a temporary string variable
    WordCount.Text = WordCount.Text + " ||| " + temp_sentence_string;
    word_count = temp_sentence_string.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //create a word count array with one word as a singular element in the array

    //We have the Word Count Array now. We can go through it with a loop, right?

    for (int j = 0; j < word_count.Length; j++)
    {
        Boolean FoundIt = File
         .ReadLines(@"c:\wordfilelib\goodwords.txt")               // <- Your file name
             .Any(line => line.Contains(word_count[j]));

        WordTest.Text = WordTest.Text + FoundIt + "(" + word_count[j] + ")";
    }

    Repeater2.DataSource = word_count;
    Repeater2.DataBind();
}

1 个答案:

答案 0 :(得分:0)

第一部分

您正在将StreamReader传递给Find函数。必须重置StreamReader才能多次使用。使用以下句子进行测试,您将看到结果。

"good good good good"

你会得到

good{true}good{false}good{false}good{false}

我建议只将文件读入数组一次,然后对数组进行处理。

using System.Linq
using System.Collections.Generic;

public class WordFinder
{
    private static bool FindWordInLines(string word_to_find, string[] lines)
    {
        foreach(var line in lines)
        {
             if(line.Contains(word_to_find)
                return true;
        }
        return false;
    }  

    public string SearchForWordsInFile(string path, string sentence)
    {
         // https://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx
         var lines = System.IO.File.ReadAllLines(path);

         var words = sentence.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); 

         var result = string.Empty;
         foreach(var word in words)
         {
             var found = FindWordInLines(word, lines);
             // {{ in string.Format outputs {
             // }} in string.Format outputs }
             // {0} says use first parameter's ToString() method
             result += string.Format("{{{0}}}", found);
         }
         return result;
    }
}

第二部分:

如果你在for循环中绑定它,它只会绑定到最后一个结果。如果在外部循环中累积结果,则可以将累积的结果传递给转发器并绑定到循环外部。

我在下面创建了一个包含两个循环的示例循环类。 &#34; resultList&#34;是累积结果的变量。

using System.Collections.Generic;

public class LoopExample
{
     public void RunLoopExample()
     {
          var outerList = new string[]{"the", "quick", "brown", "fox"};
          var innerList = new string[]{"jumps", "over", "the", "lazy", "dog"};
          // define the resultList variable outside the outer loop
          var resultList = new List<string>();
          for(int outerIndex = 0; outerIndex < outerList.Length; outerIndex ++)
          {
              var outerValue = outerList[outerIndex];
              for(int innerIndex = 0; innerIndex < innerList.Length; innerIndex++)
              {
                  var innerValue = innerList[innerIndex];
                  resultList.Add(string.Format("{0}->{1}; ", outerValue, innerValue));
              }
          }
          // use the resultList variable outside the outer loop
          foreach(var result in resultList )
          {
              Console.WriteLine(result);
          }
     }
}

在您的示例中,您将dataSource设置为resultList

Repeater2.DataSource = resultList;
Repeater2.DataBind();