1. 判断字符串是否全部都是空格
//A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and next line characters (U+000A–U+000D,U+0085).
NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
//Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
if ([trimedString length] == 0) {
NSLog(@"this string if full of whitespace");
} else {
NSLog(@"this string if full of whitespace");
}
在swift中使用,需要将String转换为NSString,如下代码,
guard let inputText = inputTextField.text as NSString? else { return }
let set = NSCharacterSet.whitespacesAndNewlines
if inputText.trimmingCharacters(in: set).count == 0 {
Toast.showText("输入内容不能为空格", duration: 0.5)
return
}
网友评论