swift对String有修剪方法吗?例如:
let result = " abc ".trim()
// result == "abc"
答案 0 :(得分:775)
以下是从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"
希望这有帮助。
答案 1 :(得分:122)
将此代码放在项目的文件中,例如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"
答案 2 :(得分:55)
在Swift 3.0中
extension String
{
func trim() -> String
{
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
}
你可以致电
let result = " Hello World ".trim() /* result = "Hello World" */
答案 3 :(得分:39)
i
答案 4 :(得分:19)
let result = " abc ".trimmingCharacters(in: .whitespacesAndNewlines)
答案 5 :(得分:8)
您可以在我写的https://bit.ly/JString。
的Swift String扩展中使用trim()方法var string = "hello "
var trimmed = string.trim()
println(trimmed)// "hello"
答案 6 :(得分:7)
是的,你可以这样做:
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"
答案 7 :(得分:6)
将字符串截断为特定长度
如果您输入了句子/文本块,并且只想从文本中保存指定的长度。将以下扩展名添加到Class
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
使用
<select name="nature" id="nature">
<option value ="Other" {% if inforequest.nature == 'Other' %} selected="selected" {% endif %}>Other</option>
<option value ="Adn" {% if inforequest.nature == 'Adn' %} selected="selected" {% endif %}>ADN</option>
</select>
答案 8 :(得分:5)
另一种类似的方式:
extension String {
var trimmed:String {
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
}
使用:
let trimmedString = "myString ".trimmed
答案 9 :(得分:4)
在Swift3 XCode 8 Final
中 请注意CharacterSet.whitespaces
不再是函数了!
(两者都不是NSCharacterSet.whitespaces
)
extension String {
func trim() -> String {
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
}
答案 10 :(得分:3)
ACTION_DOWN
取自我的回购:https://github.com/goktugyil/EZSwiftExtensions/commit/609fce34a41f98733f97dfd7b4c23b5d16416206
答案 11 :(得分:1)
// Swift 4.0删除空格和换行符
extension String {
func trim() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
答案 12 :(得分:1)
您还可以发送要修剪的字符
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: ","))
答案 13 :(得分:0)
我创建了这个函数,允许输入一个字符串并返回由任何字符
修剪的字符串列表 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中编译并完美地工作)
答案 14 :(得分:0)
不要忘记import Foundation
或UIKit
。
import Foundation
let trimmedString = " aaa "".trimmingCharacters(in: .whitespaces)
print(trimmedString)
结果:
"aaa"
否则,您将得到:
error: value of type 'String' has no member 'trimmingCharacters'
return self.trimmingCharacters(in: .whitespaces)
答案 15 :(得分:-1)
**Swift 4**
extension String {
func trimAllSpace() -> String {
return components(separatedBy: .whitespacesAndNewlines).joined()
}
func trimSpace() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
**Use:**
let result = " abc ".trimAllSpace()
// result == "abc"
let ex = " abc cd ".trimSpace()
// ex == "abc cd"