1.字符串创建
NSString *str = [[NSString alloc]init]; str = @"你好"; NSString *str1 = [[NSString alloc]initWithString:@"你好"];;``` //2格式化创建字符串 int age = 10; NSString *AgeStr = [NSString stringWithFormat:@"%d",age];2.字符串比较
//isEqualTo比较法 BOOL result = [strOne isEqualToString:strTwo]; //compare比较法 BOOL result1 = [strOne compare:strTwo];3大小写转换
NSString *string = @"This is an iOS"; 小写转大写 [string lowercaseString]; 大写转小写 [string uppercaseString]; 首字母大写 [string capitalizedString];4字符串截取
下标截取法从头开始 NSString *substring = [string substringToIndex:4]; 从4该标开始,一直到最后 NSString *substring1 = [string substringFromIndex:4]; 从范围开始(5,1),从下标5开始h往后截取1位数; NSString *substring2 = [string substringWithRange:NSMakeRange(5, 1)5判断字符串是否包含
//第一种 if ( [string1 containsString:string2]) { }else{ } //第二种 if ([string1 rangeOfString:string2].location ==NSNotFound) { //不包含 }else{ //包含 }6字符串前后缀判断
//判断前缀 NSString *string3 = @"www.baidu.com"; //前缀 BOOL ishasP = [string3 hasPrefix:@"www"]; //后缀 BOOL ishasS = [string3 hasSuffix:@"com"];7分割字符串
//分割字符串 NSString *string3 = @"www.baidu.com"; NSArray *ar = [string3 componentsSeparatedByString:@"."]; NSString *arstr1 = [ar objectAtIndex:0]; NSString *arstr2 = [ar objectAtIndex:1]; NSString *arstr3 = [ar objectAtIndex:2];8.替换字符串
//替换某个字符或某段字符串 NSString *strUrl = [string3 stringByReplacingOccurrencesOfString:@"." withString:@"1"]; //替换某段字符串 NSString *strUrlRang = [string3 stringByReplacingCharactersInRange:NSMakeRange(3, 5) withString:@"kugou"];9插入字符串
//插入字符 NSMutableString *st = [[NSMutableString alloc]initWithString:@"I suzhou"]; [st insertString:@"love" atIndex:2]; //追加字符串 [st appendString:@"虎丘"];10删除字符串
//删除字符串 NSString *string6 = @"<I love you>"; //删除<> NSMutableString *string7 = [[NSMutableString alloc]initWithString:string6]; [string7 deleteCharactersInRange:NSMakeRange(0, 1)]; [string7 deleteCharactersInRange:NSMakeRange(string7.length-1, 1)]; //方法二 [string7 stringByReplacingOccurrencesOfString:@"<" withString:@""]; [string7 stringByReplacingOccurrencesOfString:@">" withString:@""];