C#中的字符串格式以添加填充
使用C#,您可以轻松地格式化内容并向其中添加填充。
添加填充-
const string format = "{0,-5} {1,5}";
现在,将填充添加到字符串中-
string str1 = string.Format(format, "Rank","Student"); string str2 = string.Format(format, "2","Tom");
让我们看完整的代码-
示例
using System; using System.Collections.Generic; public class Program { public static void Main() { //设置填充 const string format = "{0,-5} {1,5}"; string str1 = string.Format(format, "Rank","Student"); string str2 = string.Format(format, "2","Tom"); Console.WriteLine(str1); Console.WriteLine(str2); } }
输出结果
Rank Student 2 Tom