【问题标题】:Objective C How to detect one or multiple spaces in a NSStringObjective C 如何检测 NSString 中的一个或多个空格
【发布时间】:2011-10-09 23:38:55
【问题描述】:

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

如何解决?

2011 年 10 月 12 日编辑:

各位,谢谢。

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

【问题讨论】:

标签: objective-c nsstring


【解决方案1】:

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

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

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

【讨论】:

  • 它不是数组,我只是想检查一个字符串是空的还是被某些字符填充的。如果为空,则不会写入 nsmutablearray。如果不为空,则写入nsmutablearray。
  • 我尝试了您的代码,但我收到编译器警告:`Request for member ´length´ in something not a structure or union` 为什么会这样?
  • 您需要使用count 而不是length,因为返回的是NSArray,而不是NSString
  • 抱歉,好久没用obj-c了
  • @nam 感谢您的解决方案,但我在其他地方找到了答案。
【解决方案2】:
    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];
    }

【讨论】:

  • 公平地说,这个问题没有明确说明。每个人都认为您想丢弃带有字符但也带有空格的字符串,并且只在任何地方留下带有字符且没有空格的字符串。不过,您应该接受这个答案,因为它与给出的其他答案不同。
  • @da 对此我感到非常抱歉。下次我会尽力制定清楚。
【解决方案3】:

取决于您是在寻找任何空格还是只是空格。对于您可以使用的空间:

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

【讨论】:

  • 感谢您的回答。无论如何,我收到了关于!NSEqualRanges( [string rangeofString:@" "], NSMakeRange(NSNotFound, 0) ) )!NSEqualRanges( [string rangeofCharactersFromSet: [NSCharacterSet whitespaceCharacterSet]], NSMakeRange(NSNotFound, 0) ) ) 的编译器警告。为什么会这样?
【解决方案4】:

查看documentation 中的NSString

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

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

【讨论】:

  • 谢谢。但这不是我想要的。
【解决方案5】:

查看NSRegularExpression 类和编码示例。

【讨论】:

  • 对不起,它仅适用于 Mac。我的问题是针对 iPhone 的。
  • NSRegularExpression 在 iOS 4.0 中可用。
  • 正则表达式匹配空格可能是效率较低的方法之一,但它绝对有效!
  • 我想这取决于。如果可能有多个空格或其他不一致的条件,我会说正则表达式比尝试跟踪您遇到的空格数等要好。但是,对于这样的情况,它几乎是不必要的。 :)
【解决方案6】:
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)
}

【讨论】:

  • 如果此处没有空格,spaceCount 将为 1。
  • chown,此功能可用于其他用途。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-14
相关资源
最近更新 更多