gpt4 book ai didi

ios - 从字符串swift 3中获取特定范围的字符

转载 作者:搜寻专家 更新时间:2023-11-01 05:49:47 24 4
gpt4 key购买 nike

我想从我的字符串中获取特定字符,例如这样

var str = "hey steve @steve123, you would love this!"

// Without doing this
var theCharactersIWantFromTheString = "@steve123"

我如何从 str 中检索 @steve123

最佳答案

您可以使用正则表达式"\\B\\@\\w+"

let pattern = "\\B\\@\\w+"
let sentence = "hey steve @steve123, you would love this!"

if let range = sentence.range(of: pattern, options: .regularExpression) {
print(sentence.substring(with: range)) // "@steve123\n"
}

关于ios - 从字符串swift 3中获取特定范围的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45627412/

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