【问题标题】:How can I check if a string already contains a character in Swift [duplicate]如何检查字符串是否已包含 Swift 中的字符 [重复]
【发布时间】:2019-01-15 12:35:53
【问题描述】:

如果textField 中已有- 字符,我想检查我的应用程序。

我该怎么做?

【问题讨论】:

  • 需要更多解释您的问题吗?实际上你想在哪里检查键盘何时显示或在哪里?

标签: ios swift


【解决方案1】:

你可以使用contains()方法

let myString = "test"
if myString.contains("e"){
   let index = myString.index(of: "e")
   myString.remove(at: index!)
}

p.s 你可以从textField.text 得到myString

【讨论】:

  • 是的,这行得通。你知道,当它在文本字段中时,我如何删除 - ?
  • 查看我的更新答案
【解决方案2】:

你可以简单地:

var yourTextFieldString: String = textField.text

yourTextFieldString.contains("-")

它返回一个布尔值。

【讨论】:

    猜你喜欢
    • 2016-01-25
    • 2012-04-21
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多