How many spaces does \ t use in C #

I am creating a report using StringBuilder and the details of it need to be properly configured and aligned for which I will use

private static int paperWidth = 55; //it defines the size of paper
private static readonly string singleLine = string.Empty.PadLeft(paperWidth, '-');
StringBuilder reportLayout;
reportLayout.AppendLine("\t" + "Store Name");

      



I want to keep the name in the center and many of these more feilds using \ t
Thanks in Advance.

EDIT I want to print as. Store name in center

0


source to share


2 answers


If you are simulating what tabs look like on the terminal, you should stick with 8 spaces per tab

. The Tab character moves to the next tab. By default, there is one every 8 spaces. But in most shells you can easily edit it to be any number of spaces you want

This can be accomplished with the following code:



  string tab = "\t";
  string space = new string(' ', 8);
  StringBuilder str = new StringBuilder();
  str.AppendLine(tab + "A");
  str.AppendLine(space + "B");
  string outPut = str.ToString(); // will give two lines of equal length
  int lengthOfOP = outPut.Length; //will give you 15

      

From the above example we can say that in .Net

length \t

is calculated as1

+2


source


A Tab

is Tab

, and its value is generated by the application that displays it.

Think of a word processor where Tab

means:

Go to the next tab.

You can define tab stops!

In the center of the output is not used Tabs

, use the correct StringFormat

:

StringFormat fmt = new StringFormat() 
 { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };

      

This centers the text within the rectangle in both directions:



e.Graphics.DrawString(someText, someFont, someBrush, layoutRectangle, fmt);

      

or something like that.

But it looks like you want to embed centering within the text.

This will only work if you really know everything about the rendering process, i.e. device, font and size, as well as margins, etc.

As such, it probably won't be reliable at all, no matter what you do.

A better alternative might be to either drop plain text, or use a fixed number of spaces for "mean", "centered" and then watch that number when rendering.

If you don't have control over the rendering, it won't work.

+1


source







All Articles