1

I store 2 serialized object in 2 text file and then i read both and store it in 2 different string, and try to compare this 2 strings, the comparison failed more then one time because of the carriage return and new line difference in the end of one of the string, how i could compare both with ignoring spaces or carriage returns, I know I can compress the 2 strings and compare them, but is there anything like done by .net library, Like Icomparer, I'm not sure if this will work for me too.

thank you in advance Jp

1
  • What language are you using? Post your code. Commented Jan 25, 2011 at 20:34

4 Answers 4

5

If both values are stored as strings, the String.Trim() function will take care of your troublesome whitespace characters, or just compare whilst replacing the whitespace characters.

        string a = "string comparison\r\n";
        string b = "string comparison";

        string c = a.Trim();
        string d = b.Trim();

        if (c == d)
            Console.WriteLine("strings are equal");
        else
            Console.WriteLine("strings are not equal");

        string e = a.Replace("\r\n", "");
        string f = b.Replace("\r\n", "");

        if (e == f)
            Console.WriteLine("strings are equal");
        else
            Console.WriteLine("strings are not equal");
Sign up to request clarification or add additional context in comments.

Comments

1

A quick work around would be to replace all '\n' and '\r' character by nothing and then compare.

2 Comments

this is my plan B if no built in function for it
If there is any built in function, I bet they do replace. You can always create your own compareWithIgniore(string charToIgniore) function.
0

If you're reading line by line, just ignore any characters that represent line breaks between them. If you expect line breaks, simply convert to identical characters using a find and replace.

1 Comment

No i dont read line by line, I use readtoend function so it store the entire file into a string. and the only problem im facing is in the end of the file some sometime contain \r\n and some not and this why the comparison fails
0

You can use a simple Regular Expression that can eliminate the whitespace and compare (or do what Phillippe said). But I dont think there a built-in function to do that kind of comparison.

2 Comments

yeh this what I have in mind, i was wondering if there is any built in function
Sorry. I have not seen such a thing in .NET. There might be 3rd party libraries...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.