swift是否在String上有修剪方法?

swift是否在String上有修剪方法? 例如:

let result = " abc ".trim() // result == "abc" 

以下是如何从String的开头和结尾删除所有空格。

(用Swift 2.0testing的例子)

 let myString = " \t\t Let's trim all the whitespace \n \t \n " let trimmedString = myString.stringByTrimmingCharactersInSet( NSCharacterSet.whitespaceAndNewlineCharacterSet() ) // Returns "Let's trim all the whitespace" 

(用Swift 3.0testing的例子)

 let myString = " \t\t Let's trim all the whitespace \n \t \n " let trimmedString = myString.trimmingCharacters(in: .whitespacesAndNewlines) // Returns "Let's trim all the whitespace" 

希望这可以帮助。

把这个代码放在你的项目的文件上,像Utils.swift:

 extension String { func trim() -> String { return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) } } 

所以你将能够做到这一点:

 let result = " abc ".trim() // result == "abc" 

Swift 3.0解决scheme

 extension String { func trim() -> String { return self.trimmingCharacters(in: NSCharacterSet.whitespaces) } } 

所以你将能够做到这一点:

 let result = " Hello World ".trim() // result = "HelloWorld" 

在Swift 3.0中

 extension String { func trim() -> String { return self.trimmingCharacters(in: NSCharacterSet.whitespaces) } } 

你可以打电话

 let result = " Hello World ".trim() /*result = "HelloWorld"*/ 

Swift 3

 let result = " abc ".trimmingCharacters(in: .whitespacesAndNewlines) 

你可以在我写的https://bit.ly/JString的Swiftstring扩展中使用trim()方法。;

 var string = "hello " var trimmed = string.trim() println(trimmed)// "hello" 

是的,你可以这样做:

 var str = " this is the answer " str = str.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) print(srt) // "this is the answer" 

CharacterSet实际上是一个非常强大的工具,可以创build一个修剪规则,比预先定义的设置(如.whitespacesAndNewlines)具有更多的灵活性。

例如:

 var str = " Hello World !" let cs = CharacterSet.init(charactersIn: " !") str = str.trimmingCharacters(in: cs) print(str) // "Hello World" 

将string截断为特定的长度

如果input了句子/文本块,并且只想保存指定长度的文本。 将以下扩展添加到Class

 extension String { func trunc(_ length: Int) -> String { if self.characters.count > length { return self.substring(to: self.characters.index(self.startIndex, offsetBy: length)) } else { return self } } func trim() -> String{ return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) } } 

使用

 var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." //str is length 74 print(str) //O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry. str = str.trunc(40) print(str) //O/P: Lorem Ipsum is simply dummy text of the 

在Swift3 XCode 8 Final中

注意CharacterSet.whitespaces不再是一个函数!

(都不是NSCharacterSet.whitespaces

 extension String { func trim() -> String { return self.trimmingCharacters(in: CharacterSet.whitespaces) } } 
 extension String { /// EZSE: Trims white space and new line characters public mutating func trim() { self = self.trimmed() } /// EZSE: Trims white space and new line characters, returns a new string public func trimmed() -> String { return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) } } 

采取从我的这个回购: https : //github.com/goktugyil/EZSwiftExtensions/commit/609fce34a41f98733f97dfd7b4c23b5d16416206

另一个类似的方法

 extension String { var trimmed:String { return self.trimmingCharacters(in: CharacterSet.whitespaces) } } 

使用:

 let trimmedString = "myString ".trimmed 

斯威夫特4

 let trimmedString = " abc ".trimmingCharacters(in: .whitespaces) //trimmedString == "abc" 

我创build了这个函数,允许input一个string,并返回任何字符修剪的string列表

  func Trim(input:String, character:Character)-> [String] { var collection:[String] = [String]() var index = 0 var copy = input let iterable = input var trim = input.startIndex.advancedBy(index) for i in iterable.characters { if (i == character) { trim = input.startIndex.advancedBy(index) // apennding to the list collection.append(copy.substringToIndex(trim)) //cut the input index += 1 trim = input.startIndex.advancedBy(index) copy = copy.substringFromIndex(trim) index = 0 } else { index += 1 } } collection.append(copy) return collection } 

因为没有find一个方法来做到这一点在迅速(编译和迅速2.0工作完美)