一膏萧、NSString獲取字符串中的最后一個(gè)字符以及操作
獲取到最后一個(gè)字符
NSString *lastString = [cityString substringFromIndex:cityString.length-1];
去除最后一個(gè)字符
cityString = [cityString substringToIndex:[cityString length]-1];
二冒窍、昵稱隱藏中間字符串
評(píng)論列表中隱藏用戶的真實(shí)昵稱懒叛,中間字符以*代表
/**
評(píng)論列表中隱藏用戶的真實(shí)昵稱烦租,中間字符以*代表
*/
- (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 校驗(yàn)表情符號(hào)
*
* @param string 校驗(yàn)字符串
*
* @return 是否含有表情符號(hào)
*/
- (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;
}