【问题标题】:Replace part of string with lower case letters - Swift用小写字母替换部分字符串 - Swift
【发布时间】:2016-06-05 02:07:05
【问题描述】:

我有一个基于 Swift 的 iOS 应用程序,其中一项功能允许您对帖子发表评论。无论如何,用户可以在他们的帖子中添加“@mentions”来标记其他人。但是我想阻止用户添加带有大写字母的用户名。

有没有我可以转换一个字符串,使@usernames 都是小写的?

例如:

我真的很喜欢和@uSerABC 一起观光(不允许)

我真的很喜欢和@userabc 一起观光(允许)

我知道 swift 中有一个名为 .lowercaseString 的字符串属性 - 但问题在于它使整个字符串小写,这不是我想要的。我只希望@username 小写。

有什么办法可以解决这个问题,必须使用.lowercase 属性。

感谢您抽出宝贵时间,丹。

【问题讨论】:

  • 您可以使用NSRegularExpression 找到@zzz(关于 SO 有几个问题),并为每个匹配项替换为小写(根据范围)

标签: ios string swift var lowercase


【解决方案1】:

这来自我用来检测主题标签的代码,我已经修改以检测提及:

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 { $0 }
        return results?.map{$0.rangeAtIndex(0)}
    }

它通过使用正则表达式检测字符串中的所有提及并返回一个 NSRange 数组,通过使用具有“提及”开头和结尾的范围,您可以轻松地将它们替换为小写版本。

【讨论】:

    【解决方案2】:

    使用以下命令将字符串一分为二 -

    let arr = myString.componentsSeparatedByString("@")
    //Convert arr[1] to lower case  
    //Append to arr[0]
    //Enjoy
    

    【讨论】:

    • 我能否知道我的解决方案被否决的原因,因为如果我提供的解决方案有任何缺陷,那么我可以尝试更好的解决方案并在此过程中学习新的东西
    【解决方案3】:

    感谢大家的帮助。最后我无法得到任何解决方案,经过大量测试,我想出了这个解决方案:

    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)       
    }
    

    它当然不是最优雅或最有效的解决方案,但它确实有效。如果有任何改进的方法,请发表评论。

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 2014-06-20
      • 1970-01-01
      • 2014-01-11
      • 2015-07-16
      • 2017-01-28
      • 2011-12-18
      • 2021-03-25
      • 2012-07-17
      相关资源
      最近更新 更多