removing white spaces at beginning and end of string

前端 未结 8 994
天涯浪人
天涯浪人 2021-01-14 01:41

I\'ve got a problem with removing whitespaces at the beginning and end of string. For e.g. I\'ve got a string like:

\\r\\n\\t- Someone will come here?\\n- I d

相关标签:
8条回答
  • 2021-01-14 02:36

    In Swift 4 Use it on any String type variable.

    extension String {
        func trimWhiteSpaces() -> String {
            let whiteSpaceSet = NSCharacterSet.whitespaces
            return self.trimmingCharacters(in: whiteSpaceSet)
        }
    }
    

    And Call it like this

    yourString.trimWhiteSpaces()
    
    0 讨论(0)
  • 2021-01-14 02:43

    let string = " exam ple "

    let trimmed = string.replacingOccurrences(of: "(^\s+)|(\s+)$", with: "", options: .regularExpression)

    print(">" + trimmed3 + "<")

    // prints >exam ple<

    0 讨论(0)
提交回复
热议问题