iOS字符串相关细节

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
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容