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() {
// set padding
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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP