NSString Flashcards
stringByAppendingString Example
NSString *welcome = @”Welcome to “;
welcome = [welcome stringByAppendingString:@”the Flatiron School!”];
NSLog(@”%@”, welcome);
string replacement example
NSString *shorthandWelcome = [welcome stringByReplacingOccurrencesOfString:stringToReplace withString:replacementString];
string prefix suffix check example
NSString *lafayette =
@”Marquis Marie-Joseph Paul Yves Roch Gilbert du Motier de Lafayette”;
BOOL isMarquis = [lafayette hasPrefix:@”Marquis”];
BOOL isLafayette = [lafayette hasSuffix:@”Lafayette”];
if (isMarquis && isLafayette) {
NSLog(@”Ladies and gentlemen, the Marquis de Lafayette”);
}
string check password example
NSString *password = @”p@ssw0rd”;
BOOL isValidPassword = [password isEqualToString:@”p@ssw0rd”];
if (isValidPassword) {
NSLog(@”Welcome to the Flatiron School!”);
}
NSString Convert to NSUInteger Example
NSString *ageString = @”29”;
NSUInteger age = [ageString integerValue];
age++; // birthday party!
NSLog(@”%lu”, age);