tabbing in C# resource file

2020-08-15 02:21发布

问题:

How do i add a TAB (\t) to a string resource ?

"\tText" doesn't work

回答1:

You have two options that I am aware of:

1) Do a string replace after reading your resource string: s = s.Replace("\\t","\t");

2) Enter the escape sequence directly into your resource string at creation time by typing Alt-012 (I think that's tab) on the numeric keypad.

Articles on the same here and here.



回答2:

You have to explicitly add the tab in. The easiest way of doing this is probably to type out your string in notepad (with the tab explicitly set in place rather then using an escape character) and copy and paste the text into the resource editor.

You will have a similar problem with newlines, the easiest way of adding them in is to - again - add newlines in explicitly by using the shift-enter key combination.



回答3:

Use the Alt Code for Tab (Alt + 009) Newlines are added using Shift + Return.

1) Open up resources file in VS. 2) Put cursor where you want the Tab character 3) Hold down Alt key 4) Press 0, 0, 9 on the numeric keypad. 5) Let go alt key.

When you click off the resource string, you will see the tabs get removed from the display, rest assured they are still there. This can be verified by opening the Resources.Designer.cs and looking at the comment for the resource string and highlighting the area where the tab was inserted.



回答4:

It's nearly six years since this thread was last modified, and the recommendation to use escapes still rules the day. For what it's worth, earlier today, I copied some text from a C# string constant into the resource string editor, and the tab got replaced by spaces. However, since the code expected to see the actual tab character, it threw an InvalidOperationException (my code, my exception!). Once again, I fell back to the tab, following the excellent instructions in the DevX article, "Another Way to Escape Sequences in .NET Resource Files," mentioned in the second citation in the accepted answer.

Moral: Don't count on the Windows Clipboard to faithfully copy your text.



回答5:

Have you tried the XML tab character?

Sorry my tab character didn't show! Must have got eaten up by the browser.

	


回答6:

\t does add an ascii tab but if you are displaying this in an html page you will not see that tab except in the page source. HTML doesn't render tabs or new-lines as non-breaking space. They all get reduced to 1 space character when displayed. Formatting HTML with whitespace is not recommended, that is what div with CSS or even Table are for. If you must add extra white space in HTML use the   repeatedly but it will not be tab stop correct and will create a nightmare if you ever copy and paste.

Alternately you can display your string data in a read-only Text Area. This will preserve your string format. Without knowing the specifics of what you are trying to do with your string or how you are creating it these are the best suggestions I can give you.

You can also create a variable but the \t works inline.

string TAB = char.ConvertFromUtf32(9).ToString();