如何从字符数组中删除这个“”?
How can I remove this "" from the character array?
这作为字符串返回给我:
result: vierhundertsiebenundachtzig
我将字符串转换为数组。
self.randomNumberText = Array(self.numberConfig.numberString)
print("\(self.randomNumberText)")
["v", "i", "e", "r", "", "h", "u", "n", "d", "e", "r", "t", "", "s", "i", "e", "b", "e", "n", "", "u", "n", "d", "", "a", "c", "h", "t", "z", "i", "g"]
单词之间有空心引号("")。我怎样才能删除这些双引号(“”)?
我将字符串投射到数组中的原因:我检查从 TextField 输入的每个字母是否拼写正确。但是,由于中间有""这个符号,我无法检查是否拼写正确。
号码配置
class NumberConfig: ObservableObject {
@Published var numberString: String = "-"
@Published var randomNumber: Int = 0 {
didSet {
self.numberString = self.numberToText(number: randomNumber)
}
}
func numberToText(number: Int) -> String {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "de")
formatter.numberStyle = .spellOut
formatter.minimumIntegerDigits = 2
let result = formatter.string(for: number) ?? "-"
print("result: \(result)")
return result
}
}
查看
import Combine
struct NumberToTextView: View {
@ObservedObject var numberConfig = NumberConfig()
@ObservedObject var textBindingManager = TextBindingManager(limit: 1)
@ObservedObject var speechConfig = SpeechConfig()
@State var randomNumber: [String.Element] = []
@State var randomNumberText: [String.Element] = []
@State var isCorrectAnswer: Bool = false
@State var isTrueStep: Bool = false
var body: some View {
....
ZStack(alignment: .topLeading) {
TextField("Sayıyı yazınız", text: $textBindingManager.text)
.padding()
.background(Color(UIColor.secondarySystemBackground))
.cornerRadius(15)
.onReceive(Just(self.textBindingManager.text), perform: { value in
let result = zip(self.randomNumberText, value).map({ [=13=] == }).reduce(true, {[=13=] && })
if result && !self.textBindingManager.text.isEmpty {
self.isTrueStep = true
print("true")
print("valeu: \(value) - \(self.randomNumberText)")
} else {
self.isTrueStep = false
print("falsee")
}
})
Circle()
.foregroundColor(self.isTrueStep ? .green : .red)
.frame(width: 10, height: 10)
.padding(5)
}
.padding(.horizontal)
...
}
}
过滤 Character
数组有点棘手,因为空字符串实际上不是字符。
不过还是有办法:过滤所有ASCII字符
self.randomNumberText = Array(self.numberConfig.numberString).filter{[=10=].isASCII }
或过滤全部letters
self.randomNumberText = Array(self.numberConfig.numberString).filter{[=11=].isLetter }
这作为字符串返回给我:
result: vierhundertsiebenundachtzig
我将字符串转换为数组。
self.randomNumberText = Array(self.numberConfig.numberString)
print("\(self.randomNumberText)")
["v", "i", "e", "r", "", "h", "u", "n", "d", "e", "r", "t", "", "s", "i", "e", "b", "e", "n", "", "u", "n", "d", "", "a", "c", "h", "t", "z", "i", "g"]
单词之间有空心引号("")。我怎样才能删除这些双引号(“”)? 我将字符串投射到数组中的原因:我检查从 TextField 输入的每个字母是否拼写正确。但是,由于中间有""这个符号,我无法检查是否拼写正确。
号码配置
class NumberConfig: ObservableObject {
@Published var numberString: String = "-"
@Published var randomNumber: Int = 0 {
didSet {
self.numberString = self.numberToText(number: randomNumber)
}
}
func numberToText(number: Int) -> String {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "de")
formatter.numberStyle = .spellOut
formatter.minimumIntegerDigits = 2
let result = formatter.string(for: number) ?? "-"
print("result: \(result)")
return result
}
}
查看
import Combine
struct NumberToTextView: View {
@ObservedObject var numberConfig = NumberConfig()
@ObservedObject var textBindingManager = TextBindingManager(limit: 1)
@ObservedObject var speechConfig = SpeechConfig()
@State var randomNumber: [String.Element] = []
@State var randomNumberText: [String.Element] = []
@State var isCorrectAnswer: Bool = false
@State var isTrueStep: Bool = false
var body: some View {
....
ZStack(alignment: .topLeading) {
TextField("Sayıyı yazınız", text: $textBindingManager.text)
.padding()
.background(Color(UIColor.secondarySystemBackground))
.cornerRadius(15)
.onReceive(Just(self.textBindingManager.text), perform: { value in
let result = zip(self.randomNumberText, value).map({ [=13=] == }).reduce(true, {[=13=] && })
if result && !self.textBindingManager.text.isEmpty {
self.isTrueStep = true
print("true")
print("valeu: \(value) - \(self.randomNumberText)")
} else {
self.isTrueStep = false
print("falsee")
}
})
Circle()
.foregroundColor(self.isTrueStep ? .green : .red)
.frame(width: 10, height: 10)
.padding(5)
}
.padding(.horizontal)
...
}
}
过滤 Character
数组有点棘手,因为空字符串实际上不是字符。
不过还是有办法:过滤所有ASCII字符
self.randomNumberText = Array(self.numberConfig.numberString).filter{[=10=].isASCII }
或过滤全部letters
self.randomNumberText = Array(self.numberConfig.numberString).filter{[=11=].isLetter }