0

I want to comparison two string. First is from the dateTimePicker, and second is from file.

string firtsdate = dateTimePicker1.Value.ToString("yyyy-MM-dd");  
string seconddate = dateTimePicker2.Value.ToString("yyyy-MM-dd"); 

string FilePath = path;

string fileContent = File.ReadAllText(FilePath);
string[] integerStrings = fileContent.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

int count = 0;

for (int n = 0; n < integerStrings.Length;)            
{
    count = integerStrings[n].Length;               
    //Console.Write(count + "\n");
    count--;                                         
    if (count > 2)                                  
    {
        string datastart;
        string dataend;

        if (integerStrings[n] == firtsdate)
        {
            datastart = integerStrings[n];
            Console.Write(datastart);
            dataend = (DateTime.Parse(datastart).AddDays(1)).ToShortDateString();
            Console.Write(dataend + "\n");
        }
        else
        {
            n = n + 7;
        }
    }
}

File looks like this:

  • 2016-07-01
  • 2016-07-02
  • 2016-07-06
  • ...

Problem is that they do not want to compare two of the same value, like 2016-07-02 == 2016-07-02 (from file).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
bengosha
  • 81
  • 2
  • 7
  • convert two strings to datetime variables and compare like this... https://msdn.microsoft.com/en-us/library/system.datetime.compare(v=vs.110).aspx – sowjanya attaluri Jul 04 '16 at 07:29
  • Did you inspect the values you are trying to compare? Maybe they aren't what you think they are. – Scott Hunter Jul 04 '16 at 07:33
  • Possible duplicate of [Compare string and object in c#](http://stackoverflow.com/questions/21278322/compare-string-and-object-in-c-sharp) – Alex K Jul 04 '16 at 07:46

2 Answers2

3

I suspect this is the problem:

string fileContent = File.ReadAllText(FilePath);
string[] integerStrings = fileContent.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

A line break on Windows is "\r\n" - so each line in your split will end in a \r. The simplest way to fix this is to just replace those two lines with:

string[] integerStrings = File.ReadAllLines(FilePath);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

If you are sure about your date time format, and strings are correct, you can compare 2 strings by Equals, or Compare. The end of line character in linux is \n (line feed) and windows is \r (carriage return), and \r\n for both, so you should split line by these characters or read file line by line.

Nam Tran
  • 643
  • 4
  • 14