Search code examples
iosstringswiftvarlowercase

Replace part of string with lower case letters - Swift


I have a Swift based iOS app and one of the features allows you to comment on a post. Anyway, users can add "@mentions" in their posts to tag other people. However I want to stop the user from adding a username with a capital letter.

Is there anyway I can convert a string, so that the @usernames are all in lowercase?

For example:

I really enjoy sightseeing with @uSerABC (not allowed)

I really enjoy sightseeing with @userabc (allowed)

I know there is a property for the string in swift called .lowercaseString - but the problem with that, is that it makes the entire string lowercase and thats not what I want. I only want the @username to be in lower case.

Is there any way around this with having to use the .lowercase property.

Thanks for your time, Dan.


Solution

  • Thanks to everyone for their help. In the end I couldn't get any of the solutions to work and after a lot of testing, I came up with this solution:

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

    Its certainly not the most elegant or efficient solution around but it does work. If there are any ways of improving it, please leave a comment.