-1

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.

wagashi
  • 894
  • 3
  • 15
  • 39

6 Answers6

6

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

nambrot
  • 2,551
  • 2
  • 19
  • 31
  • It is not array, I just want to check if a string is empty or filled by some characters. If empty, it will not be written to nsmutablearray. If not empty, then it will be written to nsmutablearray. – wagashi Oct 10 '11 at 08:40
  • I tried your code, but I get a compiler warning: ` Request for member ´length´in something not a structure or union` Why is that so? – wagashi Oct 10 '11 at 17:30
  • You need to use `count` instead of `length`, since it's an `NSArray` that is returned, not an `NSString`. –  Oct 10 '11 at 23:07
  • sorry, havent used obj-c in a while – nambrot Oct 11 '11 at 03:08
  • @nam Thanks for your solution, but I found answer somewhere else. – wagashi Oct 12 '11 at 23:01
  • This doesn't work if anything other than spaces separate your words. For instance, `@"one\ttwo"`, `@"one\ntwo"`, or `@"one,two"`. – Ky - Feb 09 '16 at 23:38
3

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.

msk
  • 8,885
  • 6
  • 41
  • 72
  • Thanks for your answer. Anyway, I got a compiler warning regarding `!NSEqualRanges( [string rangeofString:@" "], NSMakeRange(NSNotFound, 0) ) )` and `!NSEqualRanges( [string rangeofCharactersFromSet: [NSCharacterSet whitespaceCharacterSet]], NSMakeRange(NSNotFound, 0) ) )`. Why is that so? – wagashi Oct 12 '11 at 20:57
3
    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];
    }
wagashi
  • 894
  • 3
  • 15
  • 39
  • To be fair, the question wasn't clearly stated. Everyone thought you wanted to discard strings with characters but ALSO with spaces, and only leave the strings with characters and NO spaces anywhere. You should accept this answer though, since it's different to the other answers given. –  Oct 10 '11 at 23:04
  • @da I am very sorry about that. I will do my best to formulate clearly next time. – wagashi Oct 12 '11 at 20:58
2

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:

PengOne
  • 48,188
  • 17
  • 130
  • 149
1

Take a look at the NSRegularExpression class and coding examples.

CIFilter
  • 8,647
  • 4
  • 46
  • 66
  • Sorry, it is only for Mac. My question is for iPhone. – wagashi Oct 10 '11 at 08:41
  • `NSRegularExpression` is available in iOS as of 4.0. – CIFilter Oct 10 '11 at 17:44
  • Regex matching for whitespace will probably be one of the less efficient methods, but it definitely works! –  Oct 10 '11 at 23:01
  • I guess it depends. If there could possibly be multiple spaces or other inconsistent conditions, I'd say a regular expression is better than trying to keep track of how many spaces you've encountered, etc. But yes, it borders on unnecessary for a case like this. :) – CIFilter Oct 11 '11 at 02:14
1
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)
}
chown
  • 51,908
  • 16
  • 134
  • 170