Inserting a tab character into text using C#

后端 未结 9 814
南笙
南笙 2020-11-28 18:15

I\'m building an application where I should capture several values and build a text with them: Name, Age, etc.

The output will be a plain

相关标签:
9条回答
  • 2020-11-28 19:00

    In addition to the anwsers above you can use PadLeft or PadRight:

    string name = "John";
    string surname = "Smith";
    
    Console.WriteLine("Name:".PadRight(15)+"Surname:".PadRight(15));
    Console.WriteLine( name.PadRight(15) + surname.PadRight(15));
    

    This will fill in the string with spaces to the left or right.

    0 讨论(0)
  • 2020-11-28 19:03

    When using literal strings (start with @") this might be easier

    char tab = '\u0009';
    string A = "Apple";
    string B = "Bob";
    string myStr = String.Format(@"{0}:{1}{2}", A, tab, B);
    

    Would result in Apple:<tab>Bob

    0 讨论(0)
  • 2020-11-28 19:06

    There are several ways to do it. The simplest is using \t in your text. However, it's possible that \t doesn't work in some situations, like PdfReport nuget package.

    0 讨论(0)
提交回复
热议问题