1.分割字符串
NSString *string = @"This is a iOSDevTip";
分割 componentSeparatedByString
NSArray *array = [string componentsSeparatedByString:@"a"];
NSString *string1 = [array objectAtIndex:0];
NSString *string2 = [array objectAtIndex:1];
NSLog(@"string1:%@ string2:%@",string1,string2);
打印 string1:This is string2: iOSDevTip
2.追加字符串
NSMutableString *string = [[NSMutableString alloc] initWithString:@"I Love "];
添加appendString
[string appendString:@"China"];
NSLog(@"string:%@",string);
打印 string:I Love China
3.插入字符串
NSMutableString *string = [[NSMutableString alloc] initWithString:@"I China"];
插入 insertString
[string insertString:@"Love " atIndex:2];
NSLog(@"string: %@",string);
打印 string: I Love China
4.删除字符串
NSMutableString *string = [[NSMutableString alloc] initWithString:@"I love China"] ;
删除 deleteCharactersinRange
[string deleteCharactersInRange:NSMakeRange(2, 4)];
NSLog(@"String1: %@",string);
打印 String1: I China
5.判断是否包含前后缀
NSString *string = @"I love China";
BOOL isHasI = [string hasPrefix:@"I"];
BOOL isHasChina = [string hasSuffix:@"China"];
6.替换字符串
NSString *string = @"I love China";
NSString *replaceString = [string stringByReplacingOccurrencesOfString:@"love" withString:@"like"];
NSLog(@"replaceString: %@",replaceString);
打印 replaceString: I like China
7.去除字符串首尾的空格和换行符
NSString *string = @" I love China ";
NSString *text = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"text:%@",text);
text:I love China