gpt4 book ai didi

ios - 如何将整个句子中的某个单词定义为变量?

转载 作者:行者123 更新时间:2023-11-30 11:31:33 25 4
gpt4 key购买 nike

我希望名称(条目中的名称)成为名为 enteredName 的变量的分配值。

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
performAction()
return (true)
}

func performAction() {
if let entry = textField.text {
if entry == "my name is" + //a name {
var enteredName = //the name
print ("Your name is " + enteredName + " .")

}
}
}

我该怎么做?

最佳答案

你可以这样做:它将检查用户是否使用正确的格式(“我的名字是”开头)并且不区分大小写

import Foundation

let mySentence = "My NaMe IS GioR GioR"

let mySentenceLowercased = mySentence.lowercased()

let myNameOnly = mySentence.dropFirst("my name is ".count)

if let myNameLowercased = mySentenceLowercased.components(separatedBy: "my name is ").last,
myNameOnly.lowercased() == myNameLowercased {
print("Your name is " + myNameOnly) // Your name is GioR GioR
} else {
print("Input format error. The correct format is: \"My name is [your name goes here]\"")
}

例如,如果格式错误

let mySentence = "My NaMe  GioR GioR"

输出将是

//Input format error. The correct format is: "My name is [your name goes here]"

关于ios - 如何将整个句子中的某个单词定义为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202847/

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