折腾:
[未解决]swift中字符串的正则表达式处理:判断字符串是否符合某个类型
但没解决问题。
所以换用:
判断字符串是否以某种格式开始
swift string start with
Common String Manipulations in Swift
How do you use String.substringWithRange? (or, how do Ranges work in Swift?) – Stack Overflow
How do I check if a string contains another string in Swift? – Stack Overflow
import UIKitlet userIdExample = "user-ae96618f-3a5c-48a8-a799-9944a4e76ed9" let contactIdExample = "contact-1be1e474-388e-4b30-9030-40add6348cd4" let groupIdExample = "group-7f1ad5cd-8f18-4c5e-9fe4-604c3e5b1ab8" let teamIdExample = "team-d655d10c-a0dc-46f6-804d-c88866538036" let topicIdExample = "topic-a11838ca-a741-4d2d-ad5c-477c87400ad0"enum IdType{ case Invalid case User case Contact case Group case Team case Topic }func checkIdType(id:String) -> IdType { var curIdType:IdType if id.hasPrefix("user-") { curIdType = IdType.User }else if id.hasPrefix("contact-") { curIdType = IdType.Contact }else if id.hasPrefix("group-") { curIdType = IdType.Group }else if id.hasPrefix("team-") { curIdType = IdType.Team }else if id.hasPrefix("topic-") { curIdType = IdType.Topic }else{ curIdType = IdType.Invalid } return curIdType }checkIdType(userIdExample) checkIdType(contactIdExample) checkIdType(groupIdExample) checkIdType(teamIdExample) checkIdType(topicIdExample) checkIdType("xxxx-xxx") userIdExample.containsString("user-") userIdExample.hasPrefix("user-") userIdExample.startIndex //userIdExample.substringWithRange(Range(start: 0, end: 3)) |
效果:
转载请注明:在路上 » [已解决]Swift中去判断字符串是否以某种格式开始