标签:
发现太高深的写不出来了(虽然是从来没写过),总结总结基础的东西吧,让自己以后忘了也能来看看。
NSString *astring = @"This is a String!"; //固定字符串 NSString *str = [NSString stringWithFormat:@"asd%d%@",123,@"ttt"]; //拼组字符串
NSString *str = [@"asd" substringFromIndex:1]; //从第几位开始截到最后 NSString *str1 = [str substringToIndex:1]; //从开始位置截到第几位 NSString *str = [[@"asd" substringFromIndex:1] substringToIndex:1]; //从第几位开始截几个字符
NSArray *array = [@"asd|tes|mmm" componentsSeparatedByString:@"|"];
NSRange range = [@"12rasd124" rangeOfString:@"asd"]; if (range.location != NSNotFound) { NSString *str = [@"12rasd124" substringWithRange:range]; }
NSString *str = [@"asdasd" stringByReplacingOccurrencesOfString:@"asd" withString:@"ok"];
[filepath lastPathComponent]; //获得文件名 [filepath pathExtension]; //获得文件扩展名 [filtpaht stringByDeletingLastPathComponent]; //获得文件的所在文件夹路径 [filtpath stringByDeletingPathExtension]; //获得去除扩展名的文件路径
if (str.lenght > 0) //不管str是不是nil都可以这么判断
if ([@"asd" isEqualToString:@"asd"]) { }
NSString *stra = @"asd"; NSString *strb = @"dfs"; NSComparisonResult result = [stra compare:strb]; if (result == NSOrderedSame) { } else if (result == NSOrderedAscending) { } else { }
- (NSString *)md5HexDigest { const char* str = [self UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(str, strlen(str), result); NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH]; for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) { [ret appendFormat:@"%02X",result[i]]; } return ret; }
- (NSString *)stringEncode { NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(":/?#[]@!$ &‘()*+,;=\"<>%{}|\\^~`"), kCFStringEncodingUTF8)); return encodedString; } - (NSString *)stringDecode { NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "]; result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; return result; }
- (CGSize)boundingRectWithSize:(CGSize)size withTextFont:(UIFont *)font { if ([self isEqualToString:@""]) { return CGSizeMake(0, 0); } NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; paragraphStyle.lineSpacing = 0; NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy}; return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; }
- (BOOL)isEmail { NSString *emailRegEx = @"(?:[a-z0-9!#$%\\&‘*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&‘*+/=?\\^_`{|}" @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\" @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-" @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5" @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-" @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21" @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"; NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx]; return [regExPredicate evaluateWithObject:[self lowercaseString]]; }
- (NSString *)base64 { // Create NSData object NSData *nsdata = [self dataUsingEncoding:NSUTF8StringEncoding]; // Get NSString from NSData object in Base64 NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0]; // Print the Base64 encoded string return base64Encoded; } - (NSString *)base64Decoded { // NSData from the Base64 encoded str NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:self options:0]; // Decoded NSString from the NSData NSString *base64Decoded = [[NSString alloc] initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding]; return base64Decoded; }
额,差不多了,还有什么需要的,有需要的请留言,再加!!!!!!!!!
标签:
原文地址:http://my.oschina.net/iq19900204/blog/393588