一、NSString获取字符串中的最后一个字符以及操作
获取到最后一个字符
NSString *lastString = [cityString substringFromIndex:cityString.length-1];
去除最后一个字符
cityString = [cityString substringToIndex:[cityString length]-1];
二、昵称隐藏中间字符串
评论列表中隐藏用户的真实昵称,中间字符以*代表
/**
评论列表中隐藏用户的真实昵称,中间字符以*代表
*/
- (void)setHiddenNickName {
NSString *string = self.text;
NSInteger length = [string length];
if (length <= 2) {
return;
}else {
NSString *first = [string substringToIndex:1];//首字符
NSString *last = [string substringFromIndex:length-1];//尾字符
NSString *secondStr = [string substringToIndex:2];
NSString *lastSecond = [string substringFromIndex:length-2];//尾字符
BOOL firstBool = [self isContains4byteChar:secondStr];
BOOL lastBool = [self isContains4byteChar:lastSecond];
NSString *finiaFirstChar = first;
NSString *finiaLastChar = last;
if ((firstBool || lastBool) && (length == 3)) {
return;
}else if (firstBool && lastBool && (length == 4)) {
return;
}else {
if (firstBool) {
finiaFirstChar = secondStr;
}
if (lastBool) {
finiaLastChar = lastSecond;
}
}
self.text = [NSString stringWithFormat:@"%@**%@",finiaFirstChar,finiaLastChar];
}
}
判断字符串中是否包含表情
/**
* @brief 校验表情符号
*
* @param string 校验字符串
*
* @return 是否含有表情符号
*/
- (BOOL)isContains4byteChar:(NSString *)string
{
__block BOOL is4Byte = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if ([substring lengthOfBytesUsingEncoding:NSUTF8StringEncoding] >= 4) {
is4Byte = YES;
*stop = YES;
}
}];
return is4Byte;
}
网友评论