You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.2 KiB
95 lines
2.2 KiB
// |
|
// NSString+Character.m |
|
// tongxin |
|
// |
|
// Created by Apple on 2019/8/10. |
|
// Copyright © 2019年 xTT. All rights reserved. |
|
// |
|
|
|
#import "NSString+Character.h" |
|
|
|
@implementation NSString (Character) |
|
|
|
- (NSInteger)getStringLenthOfBytes |
|
{ |
|
NSInteger length = 0; |
|
for (int i = 0; i<[self length]; i++) { |
|
//截取字符串中的每一个字符 |
|
NSString *s = [self substringWithRange:NSMakeRange(i, 1)]; |
|
if ([self validateChineseChar:s]) { |
|
|
|
// NSLog(@" s 打印信息:%@",s); |
|
|
|
length +=2; |
|
}else{ |
|
length +=1; |
|
} |
|
|
|
// NSLog(@" 打印信息:%@ %ld",s,(long)length); |
|
} |
|
xLog(@"总长度:%ld", length); |
|
return length; |
|
} |
|
|
|
- (NSString *)subBytesOfstringToIndex:(NSInteger)index |
|
{ |
|
NSInteger length = 0; |
|
|
|
NSInteger chineseNum = 0; |
|
NSInteger zifuNum = 0; |
|
|
|
for (int i = 0; i<[self length]; i++) { |
|
//截取字符串中的每一个字符 |
|
NSString *s = [self substringWithRange:NSMakeRange(i, 1)]; |
|
if ([self validateChineseChar:s]) |
|
{ |
|
if (length + 2 > index) |
|
{ |
|
return [self substringToIndex:chineseNum + zifuNum]; |
|
} |
|
|
|
length +=2; |
|
|
|
chineseNum +=1; |
|
} |
|
else |
|
{ |
|
if (length +1 >index) |
|
{ |
|
return [self substringToIndex:chineseNum + zifuNum]; |
|
} |
|
length+=1; |
|
|
|
zifuNum +=1; |
|
} |
|
} |
|
return [self substringToIndex:index]; |
|
} |
|
|
|
//检测中文或者中文符号 |
|
- (BOOL)validateChineseChar:(NSString *)string |
|
{ |
|
NSString *nameRegEx = @"[\\u0391-\\uFFE5]"; |
|
if (![string isMatchesRegularExp:nameRegEx]) { |
|
return NO; |
|
} |
|
return YES; |
|
} |
|
|
|
//检测中文 |
|
- (BOOL)validateChinese:(NSString*)string |
|
{ |
|
NSString *nameRegEx = @"[\u4e00-\u9fa5]"; |
|
if (![string isMatchesRegularExp:nameRegEx]) { |
|
return NO; |
|
} |
|
return YES; |
|
} |
|
|
|
- (BOOL)isMatchesRegularExp:(NSString *)regex { |
|
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; |
|
return [predicate evaluateWithObject:self]; |
|
} |
|
|
|
|
|
@end
|
|
|