Objective C 如何检测 NSString 中的一个或多个空格

发布于 2024-12-08 20:09:09 字数 265 浏览 0 评论 0原文

我已经看到了我的任务的一个答案,但现在找不到了。 我想检测一个字符串是否有空单词并且至少包含“”或“”(两个空格)或多个空格,或者不包含。 如果没有,我会将其添加到 Nsmutablearray 中。 如果是,并且有空或至少一个空格,我希望它不要写入可变数组。

怎么解决这个问题呢?

编辑 2011 年 10 月 12 日:

伙计们,谢谢你们。

再次抱歉,我不清楚我的愿望。我想检查字符串是否为空或包含没有任何字符的空格。我已在下面发布了我的答案。

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.

How to solve this?

EDIT 12 October 2011:

Guys, thank you.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心的位置 2024-12-15 20:09:10

看一下 NSRegularExpression类和编码示例。

Take a look at the NSRegularExpression class and coding examples.

迷乱花海 2024-12-15 20:09:10
NSString *myString = @"ABC defa   jh";
int spaceCount = [[myString componentsSeparatedByString:@" "] count] - 1;

if (!spaceCount) {
    // Zero spaces, Do Something
} else if (spaceCount <= 2) {
    // 1-2 spaces add this to NSMutableArray (although the wording about what you wanted to do in each case is confusing, so adjust for your needs)
} else {
    // 3+ spaces, Do Not add this to NSMutableArray (adjust for your needs)
}
NSString *myString = @"ABC defa   jh";
int spaceCount = [[myString componentsSeparatedByString:@" "] count] - 1;

if (!spaceCount) {
    // Zero spaces, Do Something
} else if (spaceCount <= 2) {
    // 1-2 spaces add this to NSMutableArray (although the wording about what you wanted to do in each case is confusing, so adjust for your needs)
} else {
    // 3+ spaces, Do Not add this to NSMutableArray (adjust for your needs)
}
澜川若宁 2024-12-15 20:09:09

我不确定这是否是最有效的方法,但您可以拆分数组并查看长度是否大于 1:

if ([string componentsSeparatedByString:@" "].count > 1)

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

Im not sure whether it is the most performant way of doing it but you could split your array and see whether the length is greater than 1:

if ([string componentsSeparatedByString:@" "].count > 1)

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

离笑几人歌 2024-12-15 20:09:09
    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];
    }
    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];
    }
梦里°也失望 2024-12-15 20:09:09

取决于您是否正在寻找任何空白或只是空格。对于可以使用的空格:

if( [string length] == 0 ||
    !NSEqualRanges( [string rangeofString:@" "],
                    NSMakeRange(NSNotFound, 0) ) )
{
    // either the string is empty or we found a space
} else {
    // we didn't find a space and the string is at least of length 1
}

如果有空格,请使用空格字符集:

if( [string length] == 0 ||
    !NSEqualRanges( [string rangeOfCharacterFromSet:
                     [NSCharacterSet whitespaceCharacterSet]],
                    NSMakeRange(NSNotFound, 0) ) )
{
    // either the string is empty or we found a space
} else {
    // we didn't find a space and the string is at least of length 1
}

如果您愿意,请将 whitespaceCharacterSet 替换为 whitespaceAndNewlineCharacterSet

Depends if you're looking for ANY whitespace or just spaces. For spaces you can use:

if( [string length] == 0 ||
    !NSEqualRanges( [string rangeofString:@" "],
                    NSMakeRange(NSNotFound, 0) ) )
{
    // either the string is empty or we found a space
} else {
    // we didn't find a space and the string is at least of length 1
}

If any whitespace, use the whitespace character set:

if( [string length] == 0 ||
    !NSEqualRanges( [string rangeOfCharacterFromSet:
                     [NSCharacterSet whitespaceCharacterSet]],
                    NSMakeRange(NSNotFound, 0) ) )
{
    // either the string is empty or we found a space
} else {
    // we didn't find a space and the string is at least of length 1
}

Replace whitespaceCharacterSet with whitespaceAndNewlineCharacterSet if you like.

好菇凉咱不稀罕他 2024-12-15 20:09:09

查看文档 代表 NSString

具体来说,请在查找字符和子字符串部分中查找您想要的方法,可能您希望多次使用– rangeOfString:options:range:

另外,请在替换子字符串部分下查找您想要的方法,可能您想使用– stringByReplacingOccurrencesOfString:withString:options:range:

Look at the documentation for NSString.

Specifically, look under the section Finding Characters and Substrings for the method you want, probably you want to use – rangeOfString:options:range: multiple times.

Also, look under the section Replacing Substrings for the method you want, probably you want to use – stringByReplacingOccurrencesOfString:withString:options:range:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文