extension String {
/// 去掉首尾空格
var removeHeadAndTailSpace:String {
let whitespace = NSCharacterSet.whitespaces
return self.trimmingCharacters(in: whitespace)
}
/// 去掉首尾空格 包括后面的换行 \n
var removeHeadAndTailSpacePro:String {
let whitespace = NSCharacterSet.whitespacesAndNewlines
return self.trimmingCharacters(in: whitespace)
}
/// 去掉所有空格
var removeAllSapce: String {
return self.replacingOccurrences(of: " ", with: "", options: .literal, range: nil)
}
/// 去掉首尾空格 后 指定开头空格数
func beginSpaceNum(num: Int) -> String {
var beginSpace = ""
for _ in 0..<num {
beginSpace += " "
}
return beginSpace + self.removeHeadAndTailSpacePro
}
/// 字符串是否为空
static func blank(text: String) -> Bool {
// let trimmed = text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
let trimmed = text.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
return trimmed.isEmpty
}
}