Collapse sequences of white space into a single character and trim string

OS X 10.7+ and iOS 3.2+ Use the native regexp solution provided by hfossli. Otherwise Either use your favorite regexp library or use the following Cocoa-native solution: NSString *theString = @” Hello this is a long string! “; NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@”SELF != ””]; NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces]; … Read more

Detecting if an NSString contains…?

Here’s how I would do it: NSString *someString = @”Here is my string”; NSRange isRange = [someString rangeOfString:@”is ” options:NSCaseInsensitiveSearch]; if(isRange.location == 0) { //found it… } else { NSRange isSpacedRange = [someString rangeOfString:@” is ” options:NSCaseInsensitiveSearch]; if(isSpacedRange.location != NSNotFound) { //found it… } } You can easily add this as a category onto NSString: … Read more

AES Encryption for an NSString on the iPhone

Since you haven’t posted any code, it’s difficult to know exactly which problems you’re encountering. However, the blog post you link to does seem to work pretty decently… aside from the extra comma in each call to CCCrypt() which caused compile errors. A later comment on that post includes this adapted code, which works for … Read more

Remove all whitespaces from NSString

stringByTrimmingCharactersInSet only removes characters from the beginning and the end of the string, not the ones in the middle. 1) If you need to remove only a given character (say the space character) from your string, use: [yourString stringByReplacingOccurrencesOfString:@” ” withString:@””] 2) If you really need to remove a set of characters (namely not only … Read more

Objective-C and Swift URL encoding

To escape the characters you want is a little more work. Example code iOS7 and above: NSString *unescaped = @”http://www”; NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSLog(@”escapedString: %@”, escapedString); NSLog output: escapedString: http%3A%2F%2Fwww The following are useful URL encoding character sets: URLFragmentAllowedCharacterSet “#%<>[\]^`{|} URLHostAllowedCharacterSet “#%/<>?@\^`{|} URLPasswordAllowedCharacterSet “#%/:<>?@[\]^`{|} URLPathAllowedCharacterSet “#%;<>?[\]^`{|} URLQueryAllowedCharacterSet “#%<>[\]^`{|} URLUserAllowedCharacterSet “#%/:<>?@[\]^` Creating a … Read more

Capitalize or change case of an NSString in Objective-C

Here ya go: viewNoteDateMonth.text = [[displayDate objectAtIndex:2] uppercaseString]; Btw: “april” is lowercase ➔ [NSString lowercaseString] “APRIL” is UPPERCASE ➔ [NSString uppercaseString] “April May” is Capitalized/Word Caps ➔ [NSString capitalizedString] “April may” is Sentence caps ➔ (method missing; see workaround below) Hence what you want is called “uppercase“, not “capitalized“. 😉 As for “Sentence Caps” one … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)