Created
July 3, 2012 14:53
-
-
Save markd2/3040220 to your computer and use it in GitHub Desktop.
Support code for http://weblog.bignerdranch.com/708-string-and-string-what-is-string/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
// clang -Weverything -framework Foundation -o string-fun string-fun.m | |
int main (void) { | |
@autoreleasepool { | |
{ | |
// Split and join | |
NSString *splitString = @"hello:-(there:-(everybody"; | |
NSArray *split = [splitString componentsSeparatedByString: @":-("]; | |
NSLog (@"split: %@", split); | |
NSString *joinString = [split componentsJoinedByString: @":-)"]; | |
NSLog (@"%@ -> %@", splitString, joinString); | |
} | |
{ | |
// Extract substrings based on index | |
NSString *subby = @"whatsoever"; | |
NSString *first = [subby substringToIndex: 4]; // LEFT$ | |
NSString *last = [subby substringFromIndex: 6]; // RIGHT$ | |
NSRange range = NSMakeRange (4, 2); | |
NSString *middle = [subby substringWithRange: range]; // MID$ | |
NSLog (@"%@ -> %@ %@ %@", subby, first, middle, last); | |
} | |
{ | |
// Has prefix / suffix | |
NSString *tweet = @"@wookiee your pony is looking colorful today!"; | |
NSLog (@"looking at tweet: '%@'", tweet); | |
if ([tweet hasPrefix: @"@"]) NSLog (@" tweet is a reply"); | |
if ([tweet hasSuffix: @"!"]) NSLog (@" tweet is excitable"); | |
if ([tweet hasSuffix: @"?"]) NSLog (@" tweet is questionable"); | |
} | |
{ | |
// Easy concatenation | |
NSString *cookie = @"I made you a cookie. "; | |
NSString *eated = @"But I eated it"; | |
NSString *cat = [cookie stringByAppendingString: eated]; | |
NSLog (@"LOL: %@", cat); | |
cat = [@"I made you a cookie. " stringByAppendingString: eated]; | |
NSLog (@"LOL^2: %@", cat); | |
} | |
{ | |
// Quick number conversions. Skips whitespaceSet and ignores trailing | |
// characters. NSScanner / NSNumberFormatter suggested for more exact | |
// parsing of numbers. | |
NSArray *intValues = | |
[NSArray arrayWithObjects: @"1", @" 2bork", @"\t \t3greeble5", | |
@"-12", @"-", @"", nil]; | |
for (NSString *scan in intValues) { | |
NSLog (@" %@ -> %d", scan, [scan intValue]); | |
} | |
NSArray *boolValues = | |
[NSArray arrayWithObjects: | |
@"1", @"2", @"y", @" yEs", @"Yes", | |
@"0", @"n", @"\tNo", @"NO", | |
@"", @"gronk", nil]; | |
for (NSString *scan in boolValues) { | |
NSLog (@" %@ -> %d", scan, [scan boolValue]); | |
} | |
} | |
} | |
{ | |
// Common prefix | |
NSString *thing1 = @"BigNerdRanch"; | |
NSString *thing2 = @"Bigbooty, John"; | |
NSString *commonPrefix = [thing1 commonPrefixWithString: thing2 | |
options: 0]; | |
NSLog (@"%@ is common between %@ and %@", commonPrefix, thing1, thing2); | |
} | |
{ | |
// Capitalization | |
NSString *sentence = | |
@"fOUr scOrE aND sEveN-yEARs aGo ouR FAthers brouGHT Fourth."; | |
NSLog (@"original: %@", sentence); | |
NSString *capped = [sentence capitalizedString]; | |
NSLog (@" capped: %@", capped); | |
NSString *upper = [sentence uppercaseString]; | |
NSString *lower = [sentence lowercaseString]; | |
NSLog (@" upper: %@", upper); | |
NSLog (@" lower: %@", lower); | |
} | |
{ | |
// Trimming | |
// Remove whitespace from ends of a string | |
NSString *original = @"\n \t\t hello there \t\n \n\n"; | |
NSString *trimmed = | |
[original stringByTrimmingCharactersInSet: | |
[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
NSLog (@"trimmed '%@' to '%@'", original, trimmed); | |
} | |
{ | |
// Padding and truncating to length | |
NSString *original = @"I've got a bad feeling about this "; | |
NSString *shorter = [original stringByPaddingToLength: 22 | |
withString: nil | |
startingAtIndex: 0]; | |
NSLog (@" shorter: %@", shorter); | |
NSString *leader = [original stringByPaddingToLength: 40 | |
withString: @"." | |
startingAtIndex: 0]; | |
NSLog (@" leader: %@", leader); | |
NSString *longer = [original stringByPaddingToLength: 40 | |
withString: @"(:-" | |
startingAtIndex: 0]; | |
NSLog (@" longer: %@", longer); | |
NSString *phased = [original stringByPaddingToLength: 40 | |
withString: @"(:-" | |
startingAtIndex: 1]; | |
NSLog (@" phased: %@", phased); | |
// Actually using the starting index for something useful | |
// These will end up having different characters at the end | |
NSString *thing = @"thing"; | |
NSString *thingy = @"thingy"; | |
NSString *outOfPhaseThing = [thing stringByPaddingToLength: 30 | |
withString: @"-=" | |
startingAtIndex: 0]; | |
NSString *outOfPhaseThingy = [thingy stringByPaddingToLength: 30 | |
withString: @"-=" | |
startingAtIndex: 0]; | |
NSLog (@"%@", outOfPhaseThing); | |
NSLog (@"%@", outOfPhaseThingy); | |
NSString *inPhaseThing = [thing stringByPaddingToLength: 30 | |
withString: @"-=" | |
startingAtIndex: thing.length % 2]; | |
NSString *inPhaseThingy = [thingy stringByPaddingToLength: 30 | |
withString: @"-=" | |
startingAtIndex: thingy.length % 2]; | |
NSLog (@"%@", inPhaseThing); | |
NSLog (@"%@", inPhaseThingy); | |
} | |
return 0; | |
} // main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment