【发布时间】:2011-03-27 01:21:30
【问题描述】:
如何测试 NSString 是否为空?还是所有空格或零?使用单个方法调用?
【问题讨论】:
标签: iphone ios objective-c string nsstring
如何测试 NSString 是否为空?还是所有空格或零?使用单个方法调用?
【问题讨论】:
标签: iphone ios objective-c string nsstring
我正在使用这个定义,因为它适用于 nil 字符串和空字符串:
#define STR_EMPTY(str) \
str.length == 0
其实现在是这样的:
#define STR_EMPTY(str) \
(![str isKindOfClass:[NSString class]] || str.length == 0)
【讨论】:
也许你可以试试这样的:
+ (BOOL)stringIsEmpty:(NSString *)str
{
return (str == nil) || (([str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]).length == 0);
}
【讨论】:
你可以试试这样的:
@implementation NSString (JRAdditions)
+ (BOOL)isStringEmpty:(NSString *)string {
if([string length] == 0) { //string is empty or nil
return YES;
}
if(![[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) {
//string is all whitespace
return YES;
}
return NO;
}
@end
查看 ADC 上的 NSString 参考。
【讨论】:
NSString *str = nil;BOOL isStrEmpty = [str isEmpty];,那么isStrEmpty 将是NO,因为nil 与NO 相同。我使用这样的类别已经有一段时间了,让我感到困惑的是,我以前没有想到这一点,所以我可能/很可能是错的。
isPopulated而不是isEmpty
isNotEmpty。我确实喜欢使用肯定命名而不是否定命名的方法,但在这种情况下......哦,我想我懒得改变每一种用法。
我讨厌在这个异常古老的火灾上再写一篇日志,但我对编辑别人的答案持怀疑态度 - 特别是当它是选定的答案时。
Jacob 提出了一个后续问题:我如何通过单个方法调用来做到这一点?
答案是,通过创建一个类别——它基本上扩展了基本 Objective-C 类的功能——并为所有其他代码编写一个“速记”方法。
但是,从技术上讲,带有空格字符的字符串不是空的 - 它只是不包含任何可见的字形(在过去的几年中,我一直在使用一种名为 isEmptyString: 的方法:今天在阅读此问题后进行了转换、答案和评论集)。
要创建一个类别,请转到 Option+Click -> New File...(或 File -> New -> File... 或只是 command+n)-> 选择 Objective-C 类别。为类别选择一个名称(这将有助于命名它并减少未来可能发生的冲突) - 从“类别打开”下拉列表中选择 NSString - 将文件保存在某处。 (注意:文件会自动命名为 NSString+YourCategoryName.h 和 .m。)
我个人很欣赏 Objective-C 的自我记录特性;因此,我在 NSString 上创建了以下类别方法,修改了我原来的 isEmptyString: 方法并选择了更恰当地声明的方法(我相信编译器稍后会压缩代码 - 可能有点太多了)。
标题(.h):
#import <Foundation/Foundation.h>
@interface NSString (YourCategoryName)
/*! Strips the string of white space characters (inlcuding new line characters).
@param string NSString object to be tested - if passed nil or @"" return will
be negative
@return BOOL if modified string length is greater than 0, returns YES;
otherwise, returns NO */
+ (BOOL)visibleGlyphsExistInString:(NSString *)string;
@end
实施(.m):
@implementation NSString (YourCategoryName)
+ (BOOL)visibleGlyphsExistInString:(NSString *)string
{
// copying string should ensure retain count does not increase
// it was a recommendation I saw somewhere (I think on stack),
// made sense, but not sure if still necessary/recommended with ARC
NSString *copy = [string copy];
// assume the string has visible glyphs
BOOL visibleGlyphsExist = YES;
if (
copy == nil
|| copy.length == 0
|| [[copy stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0
) {
// if the string is nil, no visible characters would exist
// if the string length is 0, no visible characters would exist
// and, of course, if the length after stripping the white space
// is 0, the string contains no visible glyphs
visibleGlyphsExist = NO;
}
return visibleGlyphsExist;
}
@end
要调用该方法,请务必将 NSString+MyCategoryName.h 文件#import 到运行此类验证的 .h 或 .m(我更喜欢 .m 用于类别)类中,并执行以下操作:
NSString* myString = @""; // or nil, or tabs, or spaces, or something else
BOOL hasGlyphs = [NSString visibleGlyphsExistInString:myString];
希望涵盖所有基础。我记得当我第一次开始为 Objective-C 开发时,类别是那些“嗯?”之一。对我来说是考验——但现在我使用它们来增加可重用性。
编辑:我想,从技术上讲,如果我们要剥离字符,那么:
[[copy stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0
真的是所有需要的(它应该做类别方法所做的所有事情,包括副本),但我在这方面可能是错误的。
【讨论】:
应该更容易:
if (![[string stringByReplacingOccurencesOfString:@" " withString:@""] length]) { NSLog(@"This string is empty"); }
【讨论】:
基于 Jacob Relkin 的回答和 Jonathan 的评论:
@implementation TextUtils
+ (BOOL)isEmpty:(NSString*) string {
if([string length] == 0 || ![[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) {
return YES;
}
return NO;
}
@end
【讨论】:
这是我使用的,一个 NSString 的扩展:
+ (BOOL)isEmptyString:(NSString *)string;
// Returns YES if the string is nil or equal to @""
{
// Note that [string length] == 0 can be false when [string isEqualToString:@""] is true, because these are Unicode strings.
if (((NSNull *) string == [NSNull null]) || (string == nil) ) {
return YES;
}
string = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([string isEqualToString:@""]) {
return YES;
}
return NO;
}
【讨论】:
我用过,
+ (BOOL ) stringIsEmpty:(NSString *) aString {
if ((NSNull *) aString == [NSNull null]) {
return YES;
}
if (aString == nil) {
return YES;
} else if ([aString length] == 0) {
return YES;
} else {
aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString length] == 0) {
return YES;
}
}
return NO;
}
+ (BOOL ) stringIsEmpty:(NSString *) aString shouldCleanWhiteSpace:(BOOL)cleanWhileSpace {
if ((NSNull *) aString == [NSNull null]) {
return YES;
}
if (aString == nil) {
return YES;
} else if ([aString length] == 0) {
return YES;
}
if (cleanWhileSpace) {
aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString length] == 0) {
return YES;
}
}
return NO;
}
【讨论】: