removing white spaces at beginning and end of string

前端 未结 8 990
天涯浪人
天涯浪人 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:16

    String trimming first and last whitespaces and newlines in Swift 4+

    "  2 space  ".trimmingCharacters(in: .whitespacesAndNewlines)
    

    result: "2 space"

    0 讨论(0)
  • 2021-01-14 02:19
    //here i have used regular expression and replaced white spaces at the start and end
          let stringPassing : NSString? = "hdfjkhsdj hfjksdhf sdf "
          do {
            print("old->\(stringPassing)")
            let pattern : String = "(^\\s+)|(\\s+)$"
            let regex = try NSRegularExpression(pattern: pattern , options: [])
            let newMatched = regex.matchesInString(stringPassing! as String, options: [], range: NSMakeRange(0,stringPassing!.length))
            if(newMatched.count > 0){
    
              let modifiedString = regex.stringByReplacingMatchesInString(stringPassing! as String, options: [] , range: NSMakeRange(0,stringPassing!.length), withTemplate: "")
                  print("new->\(modifiedString)")
            }
    
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    
    0 讨论(0)
  • 2021-01-14 02:20

    You can use this extension and just call "yourString".trim()

    extension String
    {
        func trim() -> String
        {
            return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        }
    }
    
    0 讨论(0)
  • 2021-01-14 02:24

    In this code to restrict Textfield beginning white space

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            if (textField.text?.count)! == 0 && string == " " {
                return false
            }
            else{
                return true
            }
        }
    
    0 讨论(0)
  • 2021-01-14 02:25

    Your string contains not only whitespace but also new line characters.

    Use stringByTrimmingCharactersInSet with whitespaceAndNewlineCharacterSet.

    let string = "\r\n\t- Someone will come here?\n- I don't know for sure...\r\n\r\n"
    let trimmedString = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    

    In Swift 3 it's more cleaned up:

    let trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines)
    
    0 讨论(0)
  • 2021-01-14 02:33

    You could first remove from the beginning and then from the end like so:

    while true {
    if sentence.characters.first == "\r\n" || sentence.characters.first == "\n" {
        sentence.removeAtIndex(sentence.startIndex)
    } else {
        break
    }
    }
    
    while true {
    if sentence.characters.last == "\r\n" || sentence.characters.last == "\n" {
        sentence.removeAtIndex(sentence.endIndex.predecessor())
    } else {
        break
    }
    }
    
    0 讨论(0)
提交回复
热议问题