ด้วย 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