Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you write a tab (\t) inside a "@..." C# string? [duplicate]

Tags:

c#

escaping

Possible Duplicate:
C# @“” how do i insert a tab?

I'm trying to just use the tab on my keyboard but the compiler interprets the tabs as spaces. Using \t won't work either, it will interpret it as \t literally. Is it not possible or am I missing something?

string str = @"\thi";
MessageBox.Show(str); // Shows "\thi"
like image 284
Ahmet Avatar asked Sep 13 '11 17:09

Ahmet


People also ask

What is \t used for in c?

\t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line.

What is a tab character in c?

Tab characters. The most known and common tab is a horizontal tabulation (HT) or character tabulation, which in ASCII has the decimal character code of 9, and may be referred to as Ctrl + I or ^I. In C and many other programming languages the escape sequence \t can be used to put this character into a string literal.

What is the meaning of \t in C++?

'\t' is a horizontal tab . It is used for giving tab space horizontally in your output.


3 Answers

Split your string and insert a \t where you want it?

var str = @"This is a" + "\t" + @"tab";
like image 118
Adam Lear Avatar answered Nov 03 '22 13:11

Adam Lear


The whole point of a verbatim string literal is that escaping is turned off such that backslashes can be read as they are written. If you want escaping, then use a regular string literal (without the at symbol).

You could, of course, put a literal tab character (by pressing the tab key) within the string.

like image 21
Paul Ruane Avatar answered Nov 03 '22 13:11

Paul Ruane


Another option is to specify the tab as a parameter in string.Format:

string.Format(@"XX{0}XX", "\t"); // yields "XX    XX"
like image 20
Austin Salonen Avatar answered Nov 03 '22 12:11

Austin Salonen