What is the maximum length of an NSString object?

I would assume the hard limit for NSString would be NSUIntegerMax characters, since NSString’s index and size-related methods return an NSUInteger. Since all devices currently capable of running iOS are 32 bit, this means NSUIntegerMax is 2^32 – 1 and NSString can hold a little over 4.2 billion characters. As others have pointed out, though, … Read more

How to get the width of an NSString?

Here’s a relatively simple approach. Just create an NSAttributedString with the appropriate font and ask for its size: – (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font { NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width; }

Find one string in another with case insensitive in Objective-C

As similar to the answer provided in the link, but use options. See – (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask in Apple doc NSString *string = @”hello bla bla”; if ([string rangeOfString:@”BLA” options:NSCaseInsensitiveSearch].location == NSNotFound) { NSLog(@”string does not contain bla”); } else { NSLog(@”string contains bla!”); }

Check NSString for special characters

NSCharacterSet * set = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; if ([aString rangeOfCharacterFromSet:set].location != NSNotFound) { NSLog(@”This string contains illegal characters”); } You could also use a regex (this syntax is from RegexKitLite: http://regexkit.sourceforge.net ): if ([aString isMatchedByRegex:@”[^a-zA-Z0-9]”]) { NSLog(@”This string contains illegal characters”); }

How to insert a character into a NSString

You need to use NSMutableString NSMutableString *mu = [NSMutableString stringWithString:dir]; [mu insertString:@” ” atIndex:5]; or you could use those method to split your string : – substringFromIndex: – substringWithRange: – substringToIndex: and recombine them after with – stringByAppendingFormat: – stringByAppendingString: – stringByPaddingToLength:withString:startingAtIndex: But that way is more trouble that it’s worth. And since NSString is … Read more

NSString is integer?

You could use the -intValue or -integerValue methods. Returns zero if the string doesn’t start with an integer, which is a bit of a shame as zero is a valid value for an integer. A better option might be to use [NSScanner scanInt:] which returns a BOOL indicating whether or not it found a suitable … Read more