0

I have a global string variable - "word".

    string word = "";
    List<Label> labels = new List<Label>();
    int amount = 0;

That word is then defined/assigned in the following two functions by parsing a text document (new line delimited)

    void MakeLabels()
    {
        word = GetRandomWord();
        char[] chars = word.ToCharArray();
        ...
    }


    string GetRandomWord()
    {

        System.IO.StreamReader myFile = new System.IO.StreamReader(...);
        string myString = myFile.ReadToEnd();
        string[] words = myString.Split('\n');
        Random ran = new Random();
        return words[ran.Next(0, words.Length - 1)];
    }

And, finally, an event that validates the contents of a textbox against the "word" variable.

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox2.Text == word)
        {
            MessageBox.Show(...);
        }
        else
        {
            MessageBox.Show(...);
            textBox2.Text = "";
            textBox1.Focus();
            Reset();
        }

The problem I'm having is that even when textBox2 is equivalent to "word", I'm receiving the MessageBox related to the else statement. I think it has to do with the "word" variable carrying in the '\n'; meaning that textBox2.Text = apple and word = apple\n, thus the two variables not being equivalent. Any suggestions?

gpmurthy
  • 2,397
  • 19
  • 21
jbeckom
  • 105
  • 1
  • 2
  • 10
  • You should wrap your file reading code in a `using (....)`. Currently your `reader` is not being disposed at all. – Arran Nov 12 '13 at 16:55
  • Can you debug your code, and show us the value of `word` and `textBox2.Text`? – ProgramFOX Nov 12 '13 at 16:56
  • 2
    Using `Environment.NewLine` instead of a hardcoded character or sequence of characters for a new line is a good practice. – S_F Nov 12 '13 at 16:58
  • the problem might be u are spliting on '\n' and you should use Environment.NewLine or if you want to hard-code it \r\n.Now since you split on '\n' the '\r' remains.and probably only the last line is fine(without the '\r').If you set a breakpoint in the words array you should see that all lines but not the last will have '\r' at the end. – terrybozzio Nov 12 '13 at 17:12

4 Answers4

0

If you're sure that your problem is the line break at the end of the string, you should take a look at the String.Trim() method.

Dan
  • 209
  • 2
  • 6
0

1) Newlines in windows environments are \r\n, not \n. Splitting on \n is insufficient.
2) Contrary to suggestions, you cannot simply call someString.Split(Environment.NewLine).

There is no overload that simply takes a string. You could call someString.Split(Environment.NewLine.ToCharArray()) but will have additional concerns you need to consider. Say we have an input, string test = "test1\r\ntest2". If you call test.Split('\n'), the resulting array will have two elements: array[0] will be "test1\r" and array[1] will be "test2"...

But if you call test.Split(Environment.NewLine.ToCharArray()) then you'll get an array with three elements: array[0] will be "test1", array[1] will be "", and array[2] will be "test2"... Edited to add: You can combat that by calling test.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).

3) As one person suggested, calling string.Trim() would remove \r. Thus, return words[ran.Next(0, words.Length - 1)] could be changed to return words[ran.Next(0, words.Length - 1)].Trim() to eliminate the \r without making changes to your current split code.

trope
  • 181
  • 4
  • there is an overload in fact some overloads which accept a char[] or string[] so in the split method´s first parameter -new string[] {Environment.NewLine }-. – terrybozzio Nov 12 '13 at 17:35
  • Ha, just made an edit to mention that. I'd initially forgotten about it. – trope Nov 12 '13 at 17:36
  • A culmination of most of the submissions helped me on this, so thank you to all... The Split('\n') was taking care of it's part, but I was forgetting about the remaining '\r'. I had take care of that in the end submission. if (textBox2.Text == word.Trim('\r')) { MessageBox.Show(...); } – jbeckom Nov 12 '13 at 17:51
0

Use something like the following

string[] parseStr = myTest.Split(new string[]{Environment.NewLine},StringSplitOptions.None);

CRLF parsing blues in C#

Community
  • 1
  • 1
user1666620
  • 4,800
  • 18
  • 27
0
 string GetRandomWord()
  {

    string[] linse=  System .IO.File .ReadAllLines (......) ;
      string mlines = "";
        foreach (string line in linse)
          {
             if (line.Trim() != "")
                if(mlines=="")
                    mlines = line;
                else 
                mlines = mlines +"\n"+ line;
          }
        string[] words = mlines. Split('\n');

 Random ran = new Random();
    return words[ran.Next(0, words.Length - 1)];
}