Format Strings in Console.WriteLine method

前端 未结 3 512
悲&欢浪女
悲&欢浪女 2020-12-02 17:11

Im new to C# programming. Can someone please explain the following code:

Console.WriteLine( \"{0}{1,10}\", \"Face\", \"Frequency\" ); //Headings
Console.Writ         


        
3条回答
  •  臣服心动
    2020-12-02 18:13

    It adds padding to the left. Very useful for remembering the various string formatting patterns is the following cheat sheet:

    .NET String.Format Cheat Sheet

    Positive values add padding to the left, negative add padding to the right

    Sample                                 Generates
    String.Format("[{0, 10}]", "Foo");     [∙∙∙∙∙∙∙Foo]
    String.Format("[{0, 5}]", "Foo");      [∙∙Foo]
    String.Format("[{0, -5}]", "Foo");     [Foo∙∙]
    String.Format("[{0, -10}]", "Foo");    [Foo∙∙∙∙∙∙∙]
    

提交回复
热议问题