在Console.WriteLine方法格式字符串 [英] Format Strings in Console.WriteLine method

查看:385
本文介绍了在Console.WriteLine方法格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林新的C#编程。是否有人可以解释以下code:

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

Console.WriteLine( "{0}{1,10}", "Face", "Frequency" ); //Headings
Console.WriteLine( "{0,4}{1,10}",someval,anotherval);

据我所知,这个打印给定标题值的两列,而 {0} 是指给定的第一个参数。但是,什么是形式 {X,Y}

I understand that this prints two columns of values with the headings given, and {0} refers to the first argument given. But what is the meaning of the format strings of the form {x,y} ?

请帮助 谢谢

推荐答案

它增加了填充到左边。为记住不同的字符串格式化方式非常有用的是下面的备忘单:

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

.NET的String.Format小抄

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

这篇关于在Console.WriteLine方法格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆