http://www.jianshu.com/p/cc4064091925
http://www.cocoachina.com/ios/20160111/14926.html
- (NSString *)getChineseStringWithString:(NSString *)string
{
//(unicode中文编码范围是0x4e00~0x9fa5)
for (int i = 0; i < string.length; i++) {
int utfCode = 0;
void *buffer = &utfCode;
NSRange range = NSMakeRange(i, 1);
BOOL b = [string getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL];
if (b && (utfCode >= 0x4e00 && utfCode <= 0x9fa5)) {
return [string substringToIndex:i - 1];
}
}
return nil;
}
NSString *string = @"你bushi1222真正90-快乐";
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z\u4e00-\u9fa5]+" options:0 error:NULL];
result = [regular stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
result 即为截取后的结果。
#pragma mark -获取一段字符串中的中文字
+ (NSArray *)getAStringOfChineseWord:(NSString *)string
{
if (string == nil || [string isEqual:@""])
{
return nil;
}
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (int i=0; i<[string length]; i++)
{
int a = [string characterAtIndex:i];
if (a < 0x9fff && a > 0x4e00)
{
[arr addObject:[string substringWithRange:NSMakeRange(i, 1)]];
}
}
return arr;
}
#pragma mark -获取一段字符串中的中文字和中文字符
+ (NSArray *)getAStringOfChineseCharacters:(NSString *)string
{
if (string == nil || [string isEqual:@""])
{
return nil;
}
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (int i=0; i<string.length; i++)
{
NSRange range = NSMakeRange(i, 1);
NSString *subStr = [string substringWithRange:range];
const char *c = [subStr UTF8String];
if (strlen(c)==3)
{
[arr addObject:subStr];
}
}
return arr;
}
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"你的正则表达式" options:0 error:nil];
NSArray *matches = [regex matchesInString:parsedOutput.string options:0 range:NSMakeRange(0, parsedOutput.string.length)];
for(NSTextCheckingResult *result in [matches objectEnumerator]){
NSRange matchRange = [result range];
}
网友评论