Joshua
Joshua

Reputation: 1994

Core Data: How to find empty fields using NSPredicate

I have a database which contains some string fields with only whitespace in them. This is not a valid situation in my case so I wish to find these fields and replace them with a predefined string.

Is there a way using NSPredicate to get only these objects or must I iterate all objects, trimm the target field and check if length is zero?

Thanks!

Upvotes: 1

Views: 636

Answers (2)

Michael Frederick
Michael Frederick

Reputation: 16714

You could try something like this:

NSString *regex = @"\s+";
NSPredicate *regexPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

Upvotes: 4

Praveen-K
Praveen-K

Reputation: 3401

NSString *whiteSpaceStr = @"    This      line  contains many   white       space!   ";

// OR you string could be only

NSString *whiteSpaceStr = @"     ";

while ([whiteSpaceStr rangeOfString:@"  "].location != NSNotFound) {
    whiteSpaceStr = [whiteSpaceStr stringByReplacingOccurrencesOfString:@"  " withString:@" "];
}

Upvotes: 0

Related Questions