Objective C 如何检测 NSString 中的一个或多个空格 [英] Objective C How to detect one or multiple spaces in a NSString
问题描述
我已经看到了我的任务的一个答案,但现在找不到了.我想检测一个字符串是否有空词并且至少包含"或"(两个空格"或多个空格,或者不包含.如果没有,我会将其添加到 Nsmutablearray.如果是,有空或至少有一个空格,我不希望它被写入 mutablearray.
I have seen one answer for my task, but I couldnt find it now. I want to detect whether a string has empty word and contains at least "" or " " (two spaces" or more multiple spaces, OR not. If not, I would add this to Nsmutablearray. If yes with empty or at least one space, I would like it not to be written to mutablearray.
如何解决这个问题?
编辑 2011 年 10 月 12 日:
EDIT 12 October 2011:
伙计们,谢谢.
再次抱歉,我不清楚我的愿望.我想检查一个字符串是否为空或包含没有任何字符的空格.我已经在下面发布了我的答案.
Again I am sorry that I was unclear about my wish. I wanted to check if a string is empty or contains whitespaces without any character. I have posted my answer below.
推荐答案
if( bookmarked.length == 0 )
{
NSLog (@"not allowed: empty");
}
else if ([[bookmarked stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
{
NSLog (@"not allowed: whitespace(s)");
}
else
{
[bookmarklist addObject:bookmarked];
}
这篇关于Objective C 如何检测 NSString 中的一个或多个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!