struct CatView: View {
let poems: [String] = [
"床前明月光,",
"疑是地上霜。",
"举头望明月,",
"低头思故乡。"
]
var body: some View {
HStack {
ForEach(poems, id: \.self) { poem in
let chars = Array(poem.filter { $0.isLetter})
VStack {
ForEach(chars.indices, id: \.self) { index in
Text(String(chars[index]))
}
}
}
}
}
}
使用 poem.filter { $0.isLetter }
来过滤掉所有非汉字的字符。这个方法会返回一个字符串,其中只包含汉字,没有逗号、句号等标点符号。