我应该如何从字符串中删除所有前导空格?- 迅速

问题描述:

我需要一种方法来从作为空格的字符串中删除第一个字符.我正在寻找一种方法,甚至是 String 类型的扩展,我可以用它来剪切字符串的一个字符.

I need a way to remove the first character from a string which is a space. I am looking for a method or even an extension for the String type that I can use to cut out a character of a string.

要删除前导和尾随空格:

To remove leading and trailing whitespaces:

let trimmedString = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

斯威夫特 3/斯威夫特 4:

Swift 3 / Swift 4:

let trimmedString = string.trimmingCharacters(in: .whitespaces)