开发者

C# escape characters

e.g.

for (int i = 0; i < speaker.Length; i++) {
    s.WriteLine(speakerAmt[i] + \t + speakerID[i]);
}

I'm trying to get a tab character in-between speakerAmt[i] and speakerID[i]. Do escape characters need to be in " " " " (quotation marks)?

EDIT: Than开发者_运维技巧k you! in less than 50 seconds, I had about 5 answers. I'm impressed!


Do this :

for (int i = 0; i < speaker.Length; i++)
{
    s.WriteLine(speakerAmt[i] + "\t" + speakerID[i]);
}

And It's better to do following

for (int i = 0; i < speaker.Length; i++)
{
    s.WriteLine(string.Format("{0}\t{1}",speakerAmt[i],speakerID[i]);
}


escape characters are characters, therefore they needs to be in single quotes '\t'. Or you can construct a string with one escape character which makes it "\t" . Without any quotes \t is illegal.


for (int i = 0; i < speaker.Length; i++)
{
    s.WriteLine(speakerAmt[i] + "\t" + speakerID[i]);
}


Yes they do

string str = "Hello\tWorld!\n";


Yes - you need to do this:

 s.WriteLine(speakerAmt[i] + "\t" + speakerID[i]);


Yes, they need to be in a string

for (int i = 0; i < speaker.Length; i++)
{
   s.WriteLine(speakerAmt[i] + "\t" + speakerID[i]);
}


Yes, the quotes are necessary.

s.WriteLine(speakerAmt[i] + "\t" + speakerID[i]);

Why it is necessary:

Because this is an escape sequence that is part of a string. \t is . Like:

var hi = "Hello\tWorld";

The compiler just interprets this in a special manner when used in the context of a string. The \ character in a string usually denotes the beginning of an escape sequence (exception: string literals). Here is another example that also uses a tab using a different way:

var hi = "Hello\u0009World";

You can read more about strings, literals, and escape sequences as part of the documentation.


Yup!

for (int i = 0; i < speaker.Length; i++)
{
   s.WriteLine(speakerAmt[i] + "\t" + speakerID[i]);
}


since C#6, it is even better to do:

for (var i = 0; i < speaker.Length; i++)
{
    s.WriteLine($"{speakerAmt[i]}\t{speakerID[i]}");
}

It is Interpolated Strings

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜