24

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.)

1
  • 3
    No there isn't a predefined constant to use for the tab character. Look at this same question [Programmatically using tab character in .NET?] (stackoverflow.com/questions/2686536/…) By the way it is \t not /t Commented Jan 6, 2013 at 17:25

4 Answers 4

61

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

Sign up to request clarification or add additional context in comments.

2 Comments

The extent of my embarrassment at this correct answer shall go unpronounced.
@Thomas... don't be so hard on yourself. Anyone can have a sh*ty Sunday :)
5

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.

1 Comment

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.
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

3 Comments

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.
stackoverflow.com/questions/769621/… 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.
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.
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

3 Comments

This is quite lovely. I shall steal it and use it greedily.
And thanks for Escape Sequences. 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.
public const string TAB = "\t";

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.