Align String with Space

This example shows how to align strings with spaces. The example formats text to table and writes it to console output.

To align string to the right or to the left use static method String.Format. To align string to the left (spaces on the right) use formatting patern with comma (,) followed by a negative number of characters: String.Format(„{0,–10}“, text). To right alignment use a positive number: {0,10}.

Following example shows how to format text to the table. Values in the first and second column are aligned to the left and the third column is aligned to the right.

[C#]

Console.WriteLine("-------------------------------");
Console.WriteLine("First Name | Last Name | Age");
Console.WriteLine("-------------------------------");
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Bill", "Gates", 51));
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Edna", "Parker", 114));
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Johnny", "Depp", 44));
Console.WriteLine("-------------------------------");

Output string:

 -------------------------------
First Name | Last Name | Age
-------------------------------
Bill | Gates | 51
Edna | Parker | 114
Johnny | Depp | 44
-------------------------------

C#输出文字对齐,空格位数对齐的更多相关文章

  1. 关于checkbox与文字混排无法对齐的解决方法

    先前代码如下 <span style="vertical-align:middle"><input  type="checkbox" name ...

  2. inline-block 文字与图片不对齐

    文字旁边搭配图片时,发现图片比文字靠上,原来默认的情况是图片顶对齐而文字底对齐,通过设置css属性可以使得图片与文字对齐. 设置各对象的vertical-align属性,属性说明: baseline- ...

  3. 测开之路九十三:css之文字样式和段落对齐

    引用css 字体族 字体类型和尺寸 em:字体按倍数缩放 font-weight:调整文字的粗细 段落对齐:text-align 左对齐:left 右对齐:right 居中对齐:center 两边对齐 ...

  4. 音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系

    音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系 原创 2014年02月11日 13:56:51 4951 0 0 刚刚过完春节,受假期综合症影响脑袋有点发 ...

  5. iOS的UILabel设置居上对齐,居中对齐,居下对齐

    在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐.具体如下: // //  myUILabel.h ...

  6. 【css基础】文本对齐,水平对齐,垂直对齐

    先说水平对齐,那首先想到的就是text-align了,text-align:left,text-align:center,text- align:right,代表的就是左对齐,居中对齐和右对齐,需要注 ...

  7. iOS UILabel设置居上对齐,居中对齐,居下对齐

    在iOS中默认的UILabel中的文字在竖直方向上仅仅能居中对齐,博主參考国外站点.从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐.详细例如以下: // //  myUILabel ...

  8. mfc主窗口添加背景图片后,如何实现在背景图片上输出文字

    1.若是文档视图程序的话,在视图类的OnDraw(CDC* pDC)函数中调用pDC->TextOut()函数,就像平常输出文字一样.若是嫌文字的背景颜色破坏了图像,可以在输出文字之前调用pDC ...

  9. Eclipse设置Tab键缩进4个空格的步骤,也就是按一下Tab键输出四个空格

    Eclipse设置Tab键缩进4个空格的步骤,也就是按1下Tab键输出4个空格,步奏如下 1.点击 window->preference-,选择 General->Editors-> ...

随机推荐

  1. Microsoft QAS架接项目

    1,p位置玩文件后.运行程序命令是: QCSQueryLabelWithLES.exe -c %CD%\FinalQASModelDir --variant AMyMovie --outputFull ...

  2. 使用spring的AOP时产生的异常

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' ...

  3. erlang 虚机crash

    现网服务,每次更新一个服务时,另外一个集群所有node 都跟着同时重启一遍,这么调皮,这是闹哪样啊.. 看系统日志:/var/log/messages Oct 30 15:19:41 localhos ...

  4. Battleships in a Board

    Given an 2D board, count how many different battleships are in it. The battleships are represented w ...

  5. 【ASP.NET】复制单个文件同时到多个目录

    有时候,当我们更新了一个dll文件后,需要将该dll文件复制到到不同的文件夹中,手动操作会很麻烦,因此可以考虑利用程序实现. 利用powershell批量复制 示例代码如下: $source=&quo ...

  6. HTML页面如何判断是手机访问还是电脑访问

    可以通过js来判断访问设备,代码如下: <script type="text/javascript"> var system ={}; var p = navigato ...

  7. 下载旧版本的NDK

    在官网找不到旧版本的下载地址,只能取巧了. 写该随笔的时候,NDK最新的版本是r12,见 https://developer.android.com/ndk/downloads/index.html# ...

  8. 关于chart.js 设置canvas的宽度为父级元素的宽度的百分百 以及 X轴上面刻度数据太多如何处理

    今天在做一个数据统计的界面的时候,需要做折线统计图,在网上找了一圈发现数据统计的插件还是不少的,本着轻量级的的原则选择了Chart.js,后来在做的过程中便遇到两个问题,以此记录下来,和刚刚接触前端的 ...

  9. seo优化urlrewrite伪静态技术

    1.下载urlrewrite-3.2.0.jar 2.在WEB-INF下增加urlrewrite.xml <?xml version="1.0" encoding=" ...

  10. 非旋treap模板

    bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...