用小写字母替换部分字符串 - Swift
Replace part of string with lower case letters - Swift
我有一个基于 Swift 的 iOS 应用程序,其中一个功能允许您对 post 发表评论。无论如何,用户可以在他们的 post 中添加“@mentions”来标记其他人。但是我想阻止用户添加带有大写字母的用户名。
有没有办法转换一个字符串,让@usernames 全部变成小写?
例如:
I really enjoy sightseeing with @uSerABC (not allowed)
I really enjoy sightseeing with @userabc (allowed)
我知道 swift 中的字符串有一个 属性 称为 .lowercaseString
- 但问题是它使整个字符串小写,这不是我想要的想。我只希望@username 小写。
有什么办法可以解决这个问题,必须使用 .lowercase
属性.
丹,谢谢你的时间。
这来自我用来检测主题标签的代码,我已经修改以检测提及:
func detectMentionsInText(text: String) -> [NSRange]? {
let mentionsDetector = try? NSRegularExpression(pattern: "@(\w+)", options: NSRegularExpressionOptions.CaseInsensitive)
let results = mentionsDetector?.matchesInString(text, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, text.utf16.count)).map { [=10=] }
return results?.map{[=10=].rangeAtIndex(0)}
}
它通过使用正则表达式和 returns NSRange 数组来检测字符串中的所有提及项,通过使用范围,您可以得到 "mention" 的开头和结尾,您可以轻松替换他们用小写版本。
使用以下命令将字符串分成两部分 -
let arr = myString.componentsSeparatedByString("@")
//Convert arr[1] to lower case
//Append to arr[0]
//Enjoy
感谢大家的帮助。最后我无法得到任何解决方案,经过大量测试,我想出了这个解决方案:
func correctStringWithUsernames(inputString: String, completion: (correctString: String) -> Void) {
// Create the final string and get all
// the seperate strings from the data.
var finalString: String!
var commentSegments: NSArray!
commentSegments = inputString.componentsSeparatedByString(" ")
if (commentSegments.count > 0) {
for (var loop = 0; loop < commentSegments.count; loop++) {
// Check the username to ensure that there
// are no capital letters in the string.
let currentString = commentSegments[loop] as! String
let capitalLetterRegEx = ".*[A-Z]+.*"
let textData = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
let capitalResult = textData.evaluateWithObject(currentString)
// Check if the current loop string
// is a @user mention string or not.
if (currentString.containsString("@")) {
// If we are in the first loop then set the
// string otherwise concatenate the string.
if (loop == 0) {
if (capitalResult == true) {
// The username contains capital letters
// so change it to a lower case version.
finalString = currentString.lowercaseString
}
else {
// The username does not contain capital letters.
finalString = currentString
}
}
else {
if (capitalResult == true) {
// The username contains capital letters
// so change it to a lower case version.
finalString = "\(finalString) \(currentString.lowercaseString)"
}
else {
// The username does not contain capital letters.
finalString = "\(finalString) \(currentString)"
}
}
}
else {
// The current string is NOT a @user mention
// so simply set or concatenate the finalString.
if (loop == 0) {
finalString = currentString
}
else {
finalString = "\(finalString) \(currentString)"
}
}
}
}
else {
// No issues pass back the string.
finalString = inputString
}
// Pass back the correct username string.
completion(correctString: finalString)
}
它当然不是最优雅或最有效的解决方案,但它确实有效。如果有任何改进的方法,请发表评论。
我有一个基于 Swift 的 iOS 应用程序,其中一个功能允许您对 post 发表评论。无论如何,用户可以在他们的 post 中添加“@mentions”来标记其他人。但是我想阻止用户添加带有大写字母的用户名。
有没有办法转换一个字符串,让@usernames 全部变成小写?
例如:
I really enjoy sightseeing with @uSerABC (not allowed)
I really enjoy sightseeing with @userabc (allowed)
我知道 swift 中的字符串有一个 属性 称为 .lowercaseString
- 但问题是它使整个字符串小写,这不是我想要的想。我只希望@username 小写。
有什么办法可以解决这个问题,必须使用 .lowercase
属性.
丹,谢谢你的时间。
这来自我用来检测主题标签的代码,我已经修改以检测提及:
func detectMentionsInText(text: String) -> [NSRange]? {
let mentionsDetector = try? NSRegularExpression(pattern: "@(\w+)", options: NSRegularExpressionOptions.CaseInsensitive)
let results = mentionsDetector?.matchesInString(text, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, text.utf16.count)).map { [=10=] }
return results?.map{[=10=].rangeAtIndex(0)}
}
它通过使用正则表达式和 returns NSRange 数组来检测字符串中的所有提及项,通过使用范围,您可以得到 "mention" 的开头和结尾,您可以轻松替换他们用小写版本。
使用以下命令将字符串分成两部分 -
let arr = myString.componentsSeparatedByString("@")
//Convert arr[1] to lower case
//Append to arr[0]
//Enjoy
感谢大家的帮助。最后我无法得到任何解决方案,经过大量测试,我想出了这个解决方案:
func correctStringWithUsernames(inputString: String, completion: (correctString: String) -> Void) {
// Create the final string and get all
// the seperate strings from the data.
var finalString: String!
var commentSegments: NSArray!
commentSegments = inputString.componentsSeparatedByString(" ")
if (commentSegments.count > 0) {
for (var loop = 0; loop < commentSegments.count; loop++) {
// Check the username to ensure that there
// are no capital letters in the string.
let currentString = commentSegments[loop] as! String
let capitalLetterRegEx = ".*[A-Z]+.*"
let textData = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
let capitalResult = textData.evaluateWithObject(currentString)
// Check if the current loop string
// is a @user mention string or not.
if (currentString.containsString("@")) {
// If we are in the first loop then set the
// string otherwise concatenate the string.
if (loop == 0) {
if (capitalResult == true) {
// The username contains capital letters
// so change it to a lower case version.
finalString = currentString.lowercaseString
}
else {
// The username does not contain capital letters.
finalString = currentString
}
}
else {
if (capitalResult == true) {
// The username contains capital letters
// so change it to a lower case version.
finalString = "\(finalString) \(currentString.lowercaseString)"
}
else {
// The username does not contain capital letters.
finalString = "\(finalString) \(currentString)"
}
}
}
else {
// The current string is NOT a @user mention
// so simply set or concatenate the finalString.
if (loop == 0) {
finalString = currentString
}
else {
finalString = "\(finalString) \(currentString)"
}
}
}
}
else {
// No issues pass back the string.
finalString = inputString
}
// Pass back the correct username string.
completion(correctString: finalString)
}
它当然不是最优雅或最有效的解决方案,但它确实有效。如果有任何改进的方法,请发表评论。