-4

I am using a string comparison to get rid of a "\r\n" which is essentially a CRLF.

if (somevalue != "\r\n")
{

}

I have seen a few suggestions/variations of this on SO but not this exactly. How do you check if a string is equal to "\r\n"? When I do it its literally looking for those in the text.

logixologist
  • 3,694
  • 4
  • 28
  • 46
  • 3
    Have you tried if (somevalue != Environment.NewLine) { .... } ? – HaukurHaf Dec 29 '16 at 15:47
  • 2
    if `somevalue` is not equal to `"\r\n"`, that condition will be true. What is actually in `somevalue`, and what are you actually trying to do here? – 15ee8f99-57ff-4f92-890c-b56153 Dec 29 '16 at 15:48
  • @EdPlunkett I am probably going down the wrong road here but it brought up an interesting question because it seemed like in a string compare if the string had a CRLF in it it would not allow me to use "\r\n" – logixologist Dec 29 '16 at 15:55
  • Technically I am trying to parse an XML file and one of the nodes when being read was being read as "\r\n ". Looking for that exact string didn't work when I checked if textReader.value was not "\r\n " – logixologist Dec 29 '16 at 15:58
  • Yes, if you compare `"\r\n"` to `"\r\n"` you will find that the runtime agrees that the two strings are equal. That's not an interesting question. My guess is that you are making up bizarre assumptions about what's in the string and refusing to check those assumptions in the debugger, because "essentially" and "seems like". And "technically". All these strange qualifiers suggest that you need to focus very much more tightly on the concrete details of what your code is doing and what's in the variables. – 15ee8f99-57ff-4f92-890c-b56153 Dec 29 '16 at 16:01
  • `"\r\n"` _is_ CRLF. Depending on how you parse the xml, the newlines may be converted to `"\n"`, see http://stackoverflow.com/questions/1793908/xmlreader-newline-n-instead-of-r-n – C.Evenhuis Dec 29 '16 at 16:01
  • How do you 'us[e] a string comparison to get rid of a "\r\n"'? What does that mean? – 15ee8f99-57ff-4f92-890c-b56153 Dec 29 '16 at 16:02
  • 2
    If you want to read XML then use a proper XML library for that, rather than trying to manually make your own from scratch. – Prix Dec 29 '16 at 16:03
  • `if (textReader.Value != "\r\n ")` – logixologist Dec 29 '16 at 16:05
  • 1
    [That is definitively not the way to go. so if the next entry have 2 extra space at the end are u going to start using a regex instead? Just use a proper XML library, it will save you hours of work and the headache to adapt each new different line to it.](https://www.google.com/search?q=c%23+read+xml+site:stackoverflow.com) – Prix Dec 29 '16 at 16:08
  • According to comments, OP wants to read XML with c#, so [Reading data from XML](http://stackoverflow.com/questions/7119806/reading-data-from-xml) -- [if you don't think its a duplicate, then kindly add proper code with sample data you're trying to parse so we can stop guessing and help you...](http://stackoverflow.com/help/mcve) – Prix Dec 29 '16 at 16:10
  • Ok. I will look through the suggestions online and redo my XML Parsing. Evidently I am doing it wrong. My feeling is if I keep going down this debug road I will end up in the reputation slaughter house. The question itself was a legitimate question about whether using a "\r" "\n" or "\r\n" when comparing using a "==" would work and that was never clearly answered on SO before from what I could find. Unfortunately taking the wrong programming approach on SO, some will see as asking a poor question and there is a distinct difference between the two. – logixologist Dec 29 '16 at 16:21
  • Because it was never cleared asked, you posted no example of data that you're trying to compare against, is it a plain \r\n or does it have more data appended to it, and ultimately you even added a space after `"\r\n "` in your comments, which made it even less clear. *But what worries me even more is the fact you're trying to read some XML data, if you had posted your XML as sample and your issue, you would have probably had your questions answered by now with much more accuracy.* – Prix Dec 29 '16 at 16:25
  • There are tons of ways to load an XML File and in my case the XML file itself probably wasn't setup correctly which is why I was having a hard time reading it in. I found a way to get it to basically work except there were some rows that in the debugger showed up as "\r\n ". Out of fear of getting slaughtered on SO for doing it incorrectly I chose to ask if the standard Carriage Return/Line Feed could be detected by simple string comparison or is something else required to do it. Its only 6 points in the grand scheme of things. – logixologist Dec 29 '16 at 16:35
  • Technically you can match \r\n against a newline however different systems will write a newline differently. You further mention CRLF which is specifically \r\n. So assuming your string is ***EXACTLY*** \r\n it will match the way you're trying to, but assuming you're receiving it from some XML data and you have the possibility of having more data attached to it, its improbable it would match. [Here is a simple manipulation of string to illustrate that](https://dotnetfiddle.net/9ncv6r) Because you're trying to match it exactly and you potentially have more data it will not be true. – Prix Dec 29 '16 at 16:37
  • I would rather post a wrong code reading XML and have people correct me and learn how to use it properly then waste hours stuck at something not learning anything at all ;) so why not just go ahead and post your XML code and some sample data and get it going... – Prix Dec 29 '16 at 16:40
  • Then add that last comment as an answer and I will accept it. That answers my question exactly. This way it can help someone else who may be making the same mistake. (the 2nd to last actually). – logixologist Dec 29 '16 at 16:41
  • @prix I am going down a different road with this but your .NET Fiddle example was perfect to help me – logixologist Dec 29 '16 at 16:43
  • Because that would not be true on some OS's – Prix Dec 29 '16 at 16:44
  • 1
    *"The question itself was a legitimate question about whether using a "\r" "\n" or "\r\n" when comparing using a "==" would work"* **That is not the question you asked.** Maybe it's what you were thinking about, but the words you typed into the box said something completely different. You were downvoted, I suspect, for refusing to tell anybody what your question actually was. – 15ee8f99-57ff-4f92-890c-b56153 Dec 29 '16 at 18:27
  • Having seen everyone's reply I realize I probably didn't word it the best. Sometimes the keyboard goes faster than the head" :) – logixologist Dec 29 '16 at 18:31

2 Answers2

-1

In my particular case I was incorrectly parsing an XML file. The key point however noted by @Prix was this:

Technically you can match \r\n against a newline however different systems will write a newline differently. You further mention CRLF which is specifically \r\n. So assuming your string is EXACTLY \r\n it will match the way you're trying to, but assuming you're receiving it from some XML data and you have the possibility of having more data attached to it, its improbable it would match.

I am re-evaluating how I am reading in my XML file from all the helpful links everyone posted in comments but I wanted to summarize the outcome incase someone else tried to do a string comparison in XML and was running into it not working.

Thank you to all that commented on this question.

logixologist
  • 3,694
  • 4
  • 28
  • 46
-2

also , you can use it :

if (somevalue != Environment.NewLine) { 
  //your code
 }
Prix
  • 19,417
  • 15
  • 73
  • 132
  • Can you please add some more context around your answer. Code-only or link-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT Dec 30 '16 at 00:04