[.net] C# Improved Substring with Escape Characters

Started by
18 comments, last by jpetrie 13 years, 6 months ago
Sure, you can't use "/n", but you can certainly use "\n", which is the proper escape sequence. Seriously, did you even try it? There's no difference between the escaped sequence and the actual character code. On most systems Environment.NewLine is simply going to either be "\n" or "\r\n" anyway.
Mike Popoloski | Journal | SlimDX
Advertisement
Quote:Original post by tinybronco
For the sake of clarity and consistency, IMHO its a better practice to always use char or Environment in the place of escape sequence that way they appear the same way any place in your code.


I'm confused. How is (char)9 more clearly a tab than '\t' ?

I'm also confused how it's more consistent. Or do you use (char)97 instead of 'a' as well?
Why use char in C#... when you have string. I guess you could if you make an array for something special.
I think you're confusing two issues here, and also have a misunderstanding about escape sequences in general. First of all, you cannot say that escape sequences "don't work". They have worked, they do work, and will continue to work perfectly fine in all cases. They simply tell the compiler to replace a given escape sequence with a predetermined character code.

Now, what a given control or class decides to do with a certain character code is completely orthogonal to the issue of escape sequences. You could write a control that uses the character 'A' to indicate a newline. You can't say that escape sequences don't work, because the issue has nothing to do with escape sequences. Instead, you could say that "this control only recognizes newlines as \r".

I'm thinking somebody once told you that using Environment.NewLine is a better practice in general than using raw character codes like '\n' (which is certainly true). I'm also thinking you took this the wrong way, and assumed that all escape sequences are better off not used, which is patently false. This is evidenced by your advice of using (char)9 instead of '\t', which is hilariously dumb, since the compiler is going to translate '\t' into (char)9 anyway. Don't believe me? Try running the following program and looking at the results:

Console.WriteLine((int)'\t');Console.WriteLine((int)((char)9));Console.WriteLine("'\t'");Console.WriteLine("'" + (char)9 + "'");


So the real issue here is that "some controls don't accept \n as a newline", not that "Escape sequences don't work in many cases that apply in that scenario." In light of the original topic, that makes your input irrelevant at best, and downright misleading in places.
Mike Popoloski | Journal | SlimDX
Quote:Original post by Mike.PopoloskiYou are completely mistaken as well. You can most certainly check the index of escape characters in C# code:
*** Source Snippet Removed ***

Outputs 4, as expected. I suggest that both you and the original poster read up on escape sequences; they are simply textual replacements at compile time.


I think I got caught in the crossfire here unless you weren't referring to me when you said "both of you". I said basically the same thing you did. There's no reason to use straight chars over escape sequences because they're changed at compile time.
Quote:Original post by tinybronco
I was very clear when I said "For the sake of clarity and consistency, IMHO its a better practice to always use char or Environment in the place of escape sequence that way they appear the same way any place in your code."


So you do prefer (char)97 to 'a' ?
I still don't get how this improves clarity.

Quote:I very specifically said it was because in certain cases (not all, but certain) using an escape sequence for a newline just doesn't work in the way you would want it to.

Replacing those escapes with (char) casted numbers will never help. You're getting "reamed" because it's at best an irrelevant, unclear, and confusing addition to your post -- and at par, a blatently bad idea. We're just addressing and focusing on the confusion to better clarify the whole.

Quote:So, Ill say it again. Why escape a newline in some places, and use Environment.Newline in others when it is just more consistent to always use Environment.Newline?

Convenience. Granted, that's generally a bad always a terrible reason, so I'll give another: Especially with internet protocols, there are situations where you do not care what the local newline convention is. You care about the convention the protocol uses. For example, in HTTP requests, or implementing the IRC protocol, I want to use "\r\n" because Environment.Newline will be wrong and a bug on Unix platforms.

It's also quite likely that your software may need to read and handle multiple newline conventions, a situation for which Enivronment.Newline is also quite useless. Consistency is great -- but Environment.Newline alone can't make newline handling consistent.
Personally I find using escape sequences a lot easier than Environment. It's just less typing. Maybe it is more useful if you are writing a word processor or something of that sort. Then again you could do something like this if you want to add a tab, for example, when you hit a tab key.

string c = "word processor text";
if (key == tab)
{
c += "\t";
}

Some people say to code one way is clearer, but I think it is just a style or personal preference. It really depends if you are working with a team or not. That way everyone can settle on one style.
Quote:Original post by Flimflam
Quote:Original post by Mike.PopoloskiYou are completely mistaken as well. You can most certainly check the index of escape characters in C# code:
*** Source Snippet Removed ***

Outputs 4, as expected. I suggest that both you and the original poster read up on escape sequences; they are simply textual replacements at compile time.


I think I got caught in the crossfire here unless you weren't referring to me when you said "both of you". I said basically the same thing you did. There's no reason to use straight chars over escape sequences because they're changed at compile time.


Nah, I said both (you and the OP), referring to tinybronco and the original poster. No "of" in there [grin]
Mike Popoloski | Journal | SlimDX
Quote:Original post by Mike.Popoloski
Quote:Original post by Flimflam
Quote:Original post by Mike.PopoloskiYou are completely mistaken as well. You can most certainly check the index of escape characters in C# code:
*** Source Snippet Removed ***

Outputs 4, as expected. I suggest that both you and the original poster read up on escape sequences; they are simply textual replacements at compile time.


I think I got caught in the crossfire here unless you weren't referring to me when you said "both of you". I said basically the same thing you did. There's no reason to use straight chars over escape sequences because they're changed at compile time.


Nah, I said both (you and the OP), referring to tinybronco and the original poster. No "of" in there [grin]


Haha, my mistake. I entirely misread that :)
N.B. that the user "tinybronco" quoted frequently in this thread has edited the content of his/her original posts into nothingness and/or deleted them. That is why the discussion seems so fragmented.

This topic is closed to new replies.

Advertisement