NSString的几个方法包括:
NSString *str = @"Hello, World!";
NSRange range = [str rangeOfString:@"World"];
if (range.location != NSNotFound) {
NSLog(@"Found at index %lu", range.location);
} else {
NSLog(@"Not found");
}
NSString *str = @"Hello, World!";
if ([str hasPrefix:@"Hello"]) {
NSLog(@"Starts with Hello");
} else {
NSLog(@"Does not start with Hello");
}
NSString *str = @"Hello, World!";
if ([str hasSuffix:@"World!"]) {
NSLog(@"Ends with World!");
} else {
NSLog(@"Does not end with World!");
}
NSString *str = @"Hello, World!";
NSString *lowercase = [str lowercaseString];
NSString *uppercase = [str uppercaseString];
NSString *capitalized = [str capitalizedString];
NSLog(@"Lowercase: %@", lowercase);
NSLog(@"Uppercase: %@", uppercase);
NSLog(@"Capitalized: %@", capitalized);
这些方法可以用于查找子字符串、判断前缀和后缀、以及改变字符串的大小写。