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");