2016-02-23 44 views
-2

我有一個基於Swift的iOS應用程序,其中一個功能允許您對帖子發表評論。無論如何,用戶可以在他們的帖子中添加「@mentions」來標記其他人。但是我想停止用戶添加一個大寫字母的用戶名。用小寫字母替換字符串的部分 - Swift

是否有反正我可以轉換一個字符串,使@usernames都是小寫?

例如:

我真的很享受與@uSerABC(不允許)觀光

我真的很享受與@userabc觀光(允許)

我知道有一個屬性對於快速調用.lowercaseString的字符串 - 但問題是,它使整個字符串小寫,這不是我想要的。我只希望@username小寫。

是否有任何解決方法,必須使用.lowercase屬性。

謝謝你的時間,丹。

+0

您可以使用'NSRegularExpression'來查找'@ zzz'(在SO上有幾個問題),並用每個匹配的小寫字母替換(根據範圍) – Larme

回答

0

感謝大家的幫助。最後,我不能得到任何的解決方案的工作和大量的測試後,我想出了這個解決方案:

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

它肯定不是最優雅的或有效的解決方案,但圍繞它的工作。如果有任何改進的方法,請發表評論。

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陣列,通過使用一個範圍你有「提及」的開始和結束,你可以很容易地用小寫版本替換它們。

0

分割字符串成兩個使用以下命令 -

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

我可以知道爲什麼降低我的投票的原因嗎?解決方案,因爲如果我提供的解決方案存在任何缺陷,那麼我可以嘗試更好的解決方案,並在過程中學習新的東西 – Abhishek729