21

This console application will write .txt files to disc.

User wants to import these .txt files into Excel such that they are formatted correctly, so I plan to use tabs.

I keep getting this nonsense "Some string /t some other string /t/t some other string". There is no Environment.Tab like there is Environment.NewLine.

How do I get the tabs and not /t into my strings?

I'm sure there's a way and it'll probably be so obvious that I'll have to call myself faint all day on response.

(I'm open to other solutions as well. I might have to use | or some other delimiter/character.)

Thomas
  • 373
  • 1
  • 2
  • 9
  • 2
    No there isn't a predefined constant to use for the tab character. Look at this same question [Programmatically using tab character in .NET?] (http://stackoverflow.com/questions/2686536/programmatically-using-tab-character-in-net) By the way it is `\t` not `/t` – Steve Jan 06 '13 at 17:25

4 Answers4

58

Tabs in strings are typically written \t, not /t. Are you escaping your string correctly?

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
4

If the purpose is to create text files which will eventually be imported int excel why don't you use comma separated values. More info http://en.wikipedia.org/wiki/Comma-separated_values

hmatar
  • 2,437
  • 2
  • 17
  • 27
  • The strings have lots of commas, so I end up with this example sentence "Use 10 grams water, 20 grams flour..." as: Column A "Use 10 grams water" and Column B "20 grams flour" when what I want is the entire sentence in Column A. – Thomas Jan 06 '13 at 17:28
  • 1
    http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file http://tools.ietf.org/html/rfc4180 Sample code for handling CSV values. Your stuff is getting separated into columns because the actual commas need to be escaped. – JLRishe Jan 06 '13 at 17:33
  • Indeed? Then I might use commas instead, because now that the "/t" issue is resolved with "\t", I'm still not getting column separation. My thanks. – Thomas Jan 06 '13 at 17:39
4

If you really feel disgusted by this \t question, why not write a simple utility class

public static class MyUtility
{
    public static string Tab
    {
        get{return "\t";}
    }
}

now you can use in your code to build your tab separated strings....

string test = "MyFirstString" + MyUtility.Tab + "MySecondString" + MyUtility.Tab .......

but, again, WHY?, there is no reason to not use the predefined standard escape sequence

Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    This is quite lovely. I shall steal it and use it greedily. – Thomas Jan 06 '13 at 17:36
  • And thanks for [Escape Sequences](http://msdn.microsoft.com/en-us/library/h21280bw.aspx). I jest in admitting that I've always wanted to know how to deal with ASCII characters in hexadecimal notation. And vertical tabs! What on earth. It's a smörgåsbord of new things. – Thomas Jan 06 '13 at 17:42
  • `public const string TAB = "\t";` – Yousha Aleayoub Dec 01 '19 at 22:40
3

Technically there is tab constant in .NET. It is in Microsoft.VisualBasic.dll.

var tab = Microsoft.VisualBasic.Constants.vbTab;

But there is no reason to use vbTab, you can just use \t with the same result.

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • 2
    You were asking for .NET tab constant. And it is there in Microsoft.VisualBasic.dll, which is the same as \t, but just MS constant. You can find a lot of character Constants in Microsoft.VisualBasic.Constants if you need. Also Microsoft.VisualBasic.dll is part of the .NET and it is not specific to VisualBasic even thought it says VisualBasic it can be used in C# as well. – Vlad Bezden Jan 06 '13 at 17:55