美文网首页
iOS字符串相关细节

iOS字符串相关细节

作者: flionel | 来源:发表于2017-12-29 13:12 被阅读17次

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
}

相关文章

网友评论

      本文标题:iOS字符串相关细节

      本文链接:https://www.haomeiwen.com/subject/hiyigxtx.html