在很多编程语言中,可以使用格式化字符串的方式来实现表格内容的对齐。以下是一些常见的编程语言的示例代码:
# 使用字符串的format方法来对齐表格内容
data = [['Name', 'Age', 'City'],
['John Doe', '25', 'New York'],
['Jane Smith', '30', 'San Francisco'],
['Bob Johnson', '35', 'Chicago']]
for row in data:
print('{:<15} {:<5} {:<15}'.format(*row))
输出结果:
Name Age City
John Doe 25 New York
Jane Smith 30 San Francisco
Bob Johnson 35 Chicago
// 使用Java的String.format方法来对齐表格内容
String[][] data = {{"Name", "Age", "City"},
{"John Doe", "25", "New York"},
{"Jane Smith", "30", "San Francisco"},
{"Bob Johnson", "35", "Chicago"}};
for (String[] row : data) {
System.out.printf("%-15s %-5s %-15s%n", row[0], row[1], row[2]);
}
输出结果:
Name Age City
John Doe 25 New York
Jane Smith 30 San Francisco
Bob Johnson 35 Chicago
#include
#include // 包含iomanip头文件
int main() {
std::string data[][3] = {{"Name", "Age", "City"},
{"John Doe", "25", "New York"},
{"Jane Smith", "30", "San Francisco"},
{"Bob Johnson", "35", "Chicago"}};
for (const auto& row : data) {
std::cout << std::setw(15) << std::left << row[0]
<< std::setw(5) << std::left << row[1]
<< std::setw(15) << std::left << row[2] << std::endl;
}
return 0;
}
输出结果:
Name Age City
John Doe 25 New York
Jane Smith 30 San Francisco
Bob Johnson 35 Chicago
以上是一些常见编程语言中对齐表格内容的示例代码,具体的实现方式可能会因编程语言和具体需求而有所差异。
上一篇:表格内容超过卡片。