码迷,mamicode.com
首页 > Windows程序 > 详细

C#输出文本树形层次,前或者后自定义空格位数

时间:2016-09-12 00:51:23      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

Indent String with Spaces

This example shows how to indent strings using method for padding in C#. To repeat spaces use method String.PadLeft. If you call „hello“.PadLeft(10) you will get the string aligned to the right: „     hello“. If you use empty string instead of the „hello“ string the result will be 10× repeated space character. This can be used to create simple Indent method.

相应的可以使用String.PadRight实现右边填充固定位数的指定字符

The Indent method:

[C#]

public static string Indent(int count)
{
    return "".PadLeft(count);
}

Test code:

[C#]

Console.WriteLine(Indent(0) + "List");
Console.WriteLine(Indent(3) + "Item 1");
Console.WriteLine(Indent(6) + "Item 1.1");
Console.WriteLine(Indent(6) + "Item 1.2");
Console.WriteLine(Indent(3) + "Item 2");
Console.WriteLine(Indent(6) + "Item 2.1");

Output string:

List
   Item 1
      Item 1.1
      Item 1.2
   Item 2
      Item 2.1

C#输出文本树形层次,前或者后自定义空格位数

标签:

原文地址:http://www.cnblogs.com/coolsundy/p/5863215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!