1、判断数组中是否存在某元素
BOOLisValue = [keyArray containsObject:Q"aaa"];
2、把字符串按逗号隔开,并保存到数组:
NSArray*keyArray=[[NSArray alloc] init];
keyArray=[@"冬瓜,西瓜,南瓜,苦瓜,丝瓜"componentsSeparatedByString:@","];
3、把数组中的取出来,拼成用逗号隔开的字符串:
NSString*n=[keyArray componentsJoinedByString:@","];
4、 NSMutableArray转化成NSArray
NSArray*phoneA=[[NSArray alloc] init];
NSMutableArray*phoneArrayss=[[NSMutableArray alloc] init];
phoneA=[phoneArrayssmutableCopy];
5、获取本地时间(大写HH获取24小时制的)
NSDateFormatter*formatter = [[NSDateFormatter alloc] init];
[formattersetDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString*timeStr=[formatter stringFromDate: [NSDate date]];
6、去除nsstring中的空格
NSCharacterSet *whitespace =[NSCharacterSet whitespaceAndNewlineCharacterSet ];
NSString * username = [mUsernameField stringValue];
username = [usernamestringByTrimmingCharactersInSet :whitespace];
7、需要判断的地方:
IsNetworking *IsNetWork = [[IsNetworking alloc] init];
BOOL isNets=[IsNetWork isNetworkReachable];
if(isNets==NO){
//没有网络
}
else{
//有网络
}
8、追加字符:
NSMutableString*string = [[NSMutableString alloc] init];
string=@“你好”;
[stringappendFormat:@"中国"];
9、字符串替换:把info中所有的<都替换成#
NSString*stroneIntro=[info stringByReplacingOccurrencesOfString:@"<"
withString:@"#"];
10、字符串比较:
BoolIsTrue=[@"Nob"isEqualToString:@"Mob"]
11、不考虑大小写比较字符串
NSString*astring01 = @"this is a String!";
NSString*astring02 = @"This is a String!";
BOOLresult = [astring01 caseInsensitiveCompare:astring02] = =NSOrderedSame;
12、改变字符串的大小写
NSString*string1 = @"A String";
NSString*string2 = @"String";
NSLog(@"string1:%@",[string1uppercaseString]);//大写
NSLog(@"string2:%@",[string2lowercaseString]);//小写
NSLog(@"string2:%@",[string2capitalizedString]);//首字母大小
13、在串中搜索子串
NSString*string1 = @"This is a string";
NSString*string2 = @"string";
NSRangerange = [string1 rangeOfString:string2];
intlocation = range.location;
intleight = range.length;
NSString*astring = [[NSString alloc] initWithString:
[NSStringstringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astringrelease];
14、 抽取子串
//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString*string1 = @"This is a string";
NSString*string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//-substringFromIndex:以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString*string1 = @"This is a string";
NSString*string2 = [string1substringFromIndex:3];
NSLog(@"string2:%@",string2);
//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString*string1 = @"This is a string";
NSString*string2 = [string1 substringWithRange:NSMakeRange(0,4)];
NSLog(@"string2:%@",string2);
15、判断字符串内是否还包含别的字符串(前缀,后缀)
//01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *)aString;
NSString*String1 = @"NSStringInformation.txt";
[String1hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") :NSLog(@"NO");
[String1hasSuffix:@".txt"] = = 1 ? NSLog(@"YES"): NSLog(@"NO");