Answers:
这是删除开头和结尾的所有空格的方法String
。
(示例使用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"
(示例使用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"
希望这可以帮助。
将此代码放在项目的文件中,例如Utils.swift:
extension String
{
func trim() -> String
{
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
}
这样您就可以执行以下操作:
let result = " abc ".trim()
// result == "abc"
Swift 3.0解决方案
extension String
{
func trim() -> String
{
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
}
}
这样您就可以执行以下操作:
let result = " Hello World ".trim()
// result = "HelloWorld"
String
?
在Swift 3.0中
extension String
{
func trim() -> String
{
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
}
你可以打电话
let result = " Hello World ".trim() /* result = "Hello World" */
let result = " abc ".trimmingCharacters(in: .whitespacesAndNewlines)
是的,您可以这样做:
var str = " this is the answer "
str = str.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
print(srt) // "this is the answer"
实际上,CharacterSet是一个强大的工具,它可以创建修剪规则,并且比.whitespacesAndNewlines这样的预定义集合具有更大的灵活性。
例如:
var str = " Hello World !"
let cs = CharacterSet.init(charactersIn: " !")
str = str.trimmingCharacters(in: cs)
print(str) // "Hello World"
将字符串截断为特定长度
如果您输入了句子/文本块,并且您只想在文本中保存指定的长度。将以下扩展添加到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
您可以在我写的https://bit.ly/JString的Swift String扩展中使用trim()方法。
var string = "hello "
var trimmed = string.trim()
println(trimmed)// "hello"
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 {
func trim() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
func trim(characterSet:CharacterSet) -> String {
return self.trimmingCharacters(in: characterSet)
}
}
validationMessage = validationMessage.trim(characterSet: CharacterSet(charactersIn: ","))
我创建了此函数,该函数允许输入字符串并返回由任何字符修饰的字符串列表
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
}
因为没有找到在swift中做到这一点的方法(在swift 2.0中可以编译并完美地工作)