本文翻译自:Does swift have a trim method on String?
Does swift have a trim method on String? swift对String有修饰方法吗? For example: 例如:
let result = " abc ".trim()
// result == "abc"
#1楼
参考:https://stackoom.com/question/1oRJb/swift对String有修饰方法吗
#2楼
Here's how you remove all the whitespace from the beginning and end of a String
. 以下是从String
的开头和结尾删除所有空格的方法。
(Example tested with Swift 2.0 .) (使用Swift 2.0测试的示例。)
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"
(Example tested with Swift 3+ .) (使用Swift 3+测试的示例。)
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"
Hope this helps. 希望这可以帮助。
#3楼
You can use the trim() method in a Swift String extension I wrote https://bit.ly/JString . 您可以在我写的https://bit.ly/JString的Swift String扩展中使用trim()方法。
var string = "hello "
var trimmed = string.trim()
println(trimmed)// "hello"
#4楼
Put this code on a file on your project, something likes Utils.swift: 将此代码放在项目的文件中,类似于Utils.swift:
extension String
{
func trim() -> String
{
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
}
So you will be able to do this: 所以你将能够做到这一点:
let result = " abc ".trim()
// result == "abc"
Swift 3.0 Solution Swift 3.0解决方案
extension String
{
func trim() -> String
{
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
}
}
So you will be able to do this: 所以你将能够做到这一点:
let result = " Hello World ".trim()
// result = "HelloWorld"
#5楼
I created this function that allows to enter a string and returns a list of string trimmed by any character 我创建了这个函数,允许输入一个字符串并返回由任何字符修剪的字符串列表
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
}
as didn't found a way to do this in swift (compiles and work perfectly in swift 2.0) 因为没有找到一种方法在swift中完成此操作(在swift 2.0中编译和完美工作)
#6楼
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())
}
}
Taken from this repo of mine: https://github.com/goktugyil/EZSwiftExtensions/commit/609fce34a41f98733f97dfd7b4c23b5d16416206 取自我的回购: https : //github.com/goktugyil/EZSwiftExtensions/commit/609fce34a41f98733f97dfd7b4c23b5d16416206