removing white spaces at beginning and end of string

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

提交回复
热议问题