发现太高深的写不出来了(虽然是从来没写过),总结总结基础的东西吧,让自己以后忘了也能来看看。
NSString *astring = @"This is a String!"; //固定字符串 NSString *str = [NSString stringWithFormat:@"asd%d%@",123,@"ttt"]; //拼组字符串
1
2
3
|
NSString *str = [@
"asd"
substringFromIndex:1];
//从第几位开始截到最后
NSString *str1 = [str substringToIndex:1];
//从开始位置截到第几位
NSString *str = [[@
"asd"
substringFromIndex:1] substringToIndex:1];
//从第几位开始截几个字符
|
1
|
NSArray *array = [@
"asd|tes|mmm"
componentsSeparatedByString:@
"|"
];
|
1
2
3
4
|
NSRange range = [@
"12rasd124"
rangeOfString:@
"asd"
];
if
(range.location != NSNotFound) {
NSString *str = [@
"12rasd124"
substringWithRange:range];
}
|
1
|
NSString *str = [@
"asdasd"
stringByReplacingOccurrencesOfString:@
"asd"
withString:@
"ok"
];
|
1
2
3
4
|
[filepath lastPathComponent];
//获得文件名
[filepath pathExtension];
//获得文件扩展名
[filtpaht stringByDeletingLastPathComponent];
//获得文件的所在文件夹路径
[filtpath stringByDeletingPathExtension];
//获得去除扩展名的文件路径
|
1
|
if
(str.lenght > 0)
//不管str是不是nil都可以这么判断
|
1
2
3
|
if
([@
"asd"
isEqualToString:@
"asd"
]) {
}
|
1
2
3
4
5
6
7
8
9
10
|
NSString *stra = @
"asd"
;
NSString *strb = @
"dfs"
;
NSComparisonResult result = [stra compare:strb];
if
(result == NSOrderedSame) {
}
else
if
(result == NSOrderedAscending) {
}
else
{
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- (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
[ret appendFormat:@
"%02X"
,result[i]];
}
return
ret;
}
|
1
2
3
4
5
6
7
8
9
10
|
- (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;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
- (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;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
- (
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]];
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
- (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;
}
|