1.常用功能使用
1)初始化
NSString *astring = @"This is a String!"; //固定字符串
NSString *str = [NSString stringWithFormat:@"asd%d%@",123,@"ttt"]; //拼组字符串
2)截取
NSString *str = [@"asd" substringFromIndex:1]; //从第几位开始截到最后
NSString *str1 = [str substringToIndex:1]; //从开始位置截到第几位
NSString *str = [[@"asd" substringFromIndex:1] substringToIndex:1]; //从第几位开始截几个字符
3)分解
NSArray *array = [@"asd|tes|mmm" componentsSeparatedByString:@"|"];
4)查找
NSRange range = [@"12rasd124" rangeOfString:@"asd"];
if (range.location != NSNotFound) {
NSString *str = [@"12rasd124" substringWithRange:range];
}
5)替换
NSString *str = [@"asdasd" stringByReplacingOccurrencesOfString:@"asd" withString:@"ok"];
6)路径相关
[filepath lastPathComponent]; //获得文件名
[filepath pathExtension]; //获得文件扩展名
[filtpaht stringByDeletingLastPathComponent]; //获得文件的所在文件夹路径
[filtpath stringByDeletingPathExtension]; //获得去除扩展名的文件路径
7)字符串是否为空
if (str.lenght > 0) //不管str是不是nil都可以这么判断
8)字符串比较
if ([@"asd" isEqualToString:@"asd"]) {
}
9)字符串比大小
NSString *stra = @"asd";
NSString *strb = @"dfs";
NSComparisonResult result = [stra compare:strb];
if (result == NSOrderedSame) {
} else if (result == NSOrderedAscending) {
} else {
}
2.常用扩展功能(类别扩展)
1)md5
- (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;
}
2)urlEncode
- (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;
}
3)sizeFromFont
- (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;
}
4) 是否是邮箱格式
- (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]];
}
5) Base64
- (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;
}