gpt4 book ai didi

ios - 如何使用 "\t"右对齐或左对齐?

转载 作者:行者123 更新时间:2023-11-29 01:08:23 28 4
gpt4 key购买 nike

我正在尝试列出姓名、出生日期和年龄。我想在一个整洁的显示中相应地打印出每个名字、出生日期和年龄。示例:

names = ["name112312", "name2123", "name3as"]
dob = ["jan112", "dec9", "augasd5"]
age = [1, 2, 123]

我要打印的内容是:

Name         DOB         Age

name112312 jan112 1

name2123 dec9 26

name3as augasd5 123

请注意每个名称的对齐方式如何相同,即使名称的长度不同也是如此。

这就是我想要做的,这就是目前正在发生的事情:

http://i.stack.imgur.com/gMbVI.png

代码:

var list_DbU: Int

var db_nameS : Array = ["Bill", "Joe", "Sam", "Cathy"]
var db_dobS : Array = ["08:02:00", "01:25:96", "12:24:01", "04:03:99"]
var db_ageS : Array = [15, 19, 14, 16]


println("Db info: ")
println("Name \t DOB \t Age")
for list_DbU = 0; list_DbU < db_nameS.count; ++list_DbU {
println("\n\(db_nameS[list_DbU]) \t \(db_dobS[list_DbU])")
}

最佳答案

使用NSString(format:)可以填充宽度,%-20s20是填充字段的宽度

var list_DbU: Int

var db_nameS : Array = ["Bill", "Joe", "Sam", "Cathy"]

var db_dobS : Array = ["08:02:00", "01:25:96", "12:24:01", "04:03:99"]
var db_ageS : Array = [15, 19, 14, 16]

print("Db info: ")
print(NSString(format: "%-20s%-20s%-20s", ("Name" as NSString).UTF8String, ("DOB" as NSString).UTF8String , ("Age" as NSString).UTF8String))
for list_DbU = 0; list_DbU < db_nameS.count; ++list_DbU {
let name = (db_nameS[list_DbU] as NSString).UTF8String
var dob = (db_dobS[list_DbU] as NSString).UTF8String
var age = db_ageS[list_DbU]
print(NSString(format: "%-20s%-20s%d", name, dob, age))
}

输出:

Db info: 
Name DOB Age
Bill 08:02:00 15
Joe 01:25:96 19
Sam 12:24:01 14
Cathy 04:03:99 16

关于ios - 如何使用 "\t"右对齐或左对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36074781/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com