Search code examples
c#.netwebclient

C# compare WebClient downloaded string always says inequal


I'm trying to compare strings and I'm using a WebClient to download one of them and when I compare them, it always returns that they aren't equal even though they are.

here's the code

using System;
using System.Net;

class MainClass {
  public static readonly string test = "e";
  public static readonly string e = "http://raw.githubusercontent.com/SeizureSaladd/test/main/ok.txt";
  public static void Main (string[] args) {
         

    if(MainClass.test == new WebClient().DownloadString(MainClass.e))
    {
      Console.WriteLine("yay it works lol");
    }
    else
    {
      Console.WriteLine("uh oh");
      Console.WriteLine(new WebClient().DownloadString(MainClass.e));
    }
  }
}

if I run this, it says uh oh then returns e even though they're exactly the same.

Does anyone know how to fix this?


Solution

  • http://raw.githubusercontent.com/SeizureSaladd/test/main/ok.txt returns: "e\n". You'll need to strip the trailing new-line character to get a valid comparison. So no, they aren't equal at all.