<pre>
import <Foundation/Foundation.h>
import <CoreGraphics/CGBase.h>
@interface NSString (QMUI)
/// 判断是否包含某个子字符串
- (BOOL)includesString:(NSString *)string;
/// 去掉头尾的空白字符
- (NSString *)trim;
/// 去掉整段文字内的所有空白字符(包括换行符)
- (NSString *)trimAllWhiteSpace;
/// 将文字中的换行符替换为空格
- (NSString *)trimLineBreakCharacter;
/// 把该字符串转换为对应的 md5
- (NSString *)md5;
/// 把某个十进制数字转换成十六进制的数字的字符串,例如“10”->“A”
- (NSString *)hexStringWithInteger:(NSInteger)integer;
/**
- 用正则表达式匹配的方式去除字符串里一些特殊字符,避免UI上的展示问题
- @link http://www.croton.su/en/uniblock/Diacriticals.html
*/
- (NSString *)removeMagicalChar;
/**
- 按照中文 2 个字符、英文 1 个字符的方式来计算文本长度
*/
- (NSUInteger)lengthWhenCountingNonASCIICharacterAsTwo;
/**
- 将字符串从指定的 index 开始裁剪到结尾,裁剪时会避免将 emoji 等 "character sequences" 拆散(一个 emoji 表情占用1-4个长度的字符)。
- 例如对于字符串“😊😞”,它的长度为4,若调用 [string substringAvoidBreakingUpCharacterSequencesFromIndex:1],将返回“😊😞”。
- 若调用系统的 [string substringFromIndex:1],将返回“?😞”。(?表示乱码,因为第一个 emoji 表情被从中间裁开了)。
- @param index 要从哪个 index 开始裁剪文字
- @param lessValue 要按小的长度取,还是按大的长度取
- @param countingNonASCIICharacterAsTwo 是否按照 英文 1 个字符长度、中文 2 个字符长度的方式来裁剪
- @return 裁剪完的字符
*/
- (NSString *)substringAvoidBreakingUpCharacterSequencesFromIndex:(NSUInteger)index lessValue:(BOOL)lessValue countingNonASCIICharacterAsTwo:(BOOL)countingNonASCIICharacterAsTwo;
/**
- 相当于
substringAvoidBreakingUpCharacterSequencesFromIndex: lessValue:YES
countingNonASCIICharacterAsTwo:NO - @see substringAvoidBreakingUpCharacterSequencesFromIndex:lessValue:countingNonASCIICharacterAsTwo:
*/
- (NSString *)substringAvoidBreakingUpCharacterSequencesFromIndex:(NSUInteger)index;
/**
- 将字符串从开头裁剪到指定的 index,裁剪时会避免将 emoji 等 "character sequences" 拆散(一个 emoji 表情占用1-4个长度的字符)。
- 例如对于字符串“😊😞”,它的长度为4,若调用 [string substringAvoidBreakingUpCharacterSequencesToIndex:1],将返回“😊”。
- 若调用系统的 [string substringToIndex:1],将返回“?”。(?表示乱码,因为第一个 emoji 表情被从中间裁开了)。
- @param index 要裁剪到哪个 index
- @return 裁剪完的字符
- @param countingNonASCIICharacterAsTwo 是否按照 英文 1 个字符长度、中文 2 个字符长度的方式来裁剪
*/
- (NSString *)substringAvoidBreakingUpCharacterSequencesToIndex:(NSUInteger)index lessValue:(BOOL)lessValue countingNonASCIICharacterAsTwo:(BOOL)countingNonASCIICharacterAsTwo;
/**
- 相当于
substringAvoidBreakingUpCharacterSequencesToIndex:lessValue:YES
countingNonASCIICharacterAsTwo:NO - @see substringAvoidBreakingUpCharacterSequencesToIndex:lessValue:countingNonASCIICharacterAsTwo:
*/
- (NSString *)substringAvoidBreakingUpCharacterSequencesToIndex:(NSUInteger)index;
/**
- 将字符串里指定 range 的子字符串裁剪出来,会避免将 emoji 等 "character sequences" 拆散(一个 emoji 表情占用1-4个长度的字符)。
- 例如对于字符串“😊😞”,它的长度为4,在 lessValue 模式下,裁剪 (0, 1) 得到的是空字符串,裁剪 (0, 2) 得到的是“😊”。
- 在非 lessValue 模式下,裁剪 (0, 1) 或 (0, 2),得到的都是“😊”。
- @param range 要裁剪的文字位置
- @param lessValue 裁剪时若遇到“character sequences”,是向下取整还是向上取整。
- @param countingNonASCIICharacterAsTwo 是否按照 英文 1 个字符长度、中文 2 个字符长度的方式来裁剪
- @return 裁剪完的字符
*/
- (NSString *)substringAvoidBreakingUpCharacterSequencesWithRange:(NSRange)range lessValue:(BOOL)lessValue countingNonASCIICharacterAsTwo:(BOOL)countingNonASCIICharacterAsTwo;
/**
- 相当于
substringAvoidBreakingUpCharacterSequencesWithRange:lessValue:YES
countingNonASCIICharacterAsTwo:NO - @see substringAvoidBreakingUpCharacterSequencesWithRange:lessValue:countingNonASCIICharacterAsTwo:
*/
- (NSString *)substringAvoidBreakingUpCharacterSequencesWithRange:(NSRange)range;
/**
- 移除指定位置的字符,可兼容emoji表情的情况(一个emoji表情占1-4个length)
- @param index 要删除的位置
*/
- (NSString *)stringByRemoveCharacterAtIndex:(NSUInteger)index;
/**
- 移除最后一个字符,可兼容emoji表情的情况(一个emoji表情占1-4个length)
- @see
stringByRemoveCharacterAtIndex:
*/
- (NSString *)stringByRemoveLastCharacter;
@end
@interface NSString (QMUI_StringFormat)
- (instancetype)stringWithNSInteger:(NSInteger)integerValue;
- (instancetype)stringWithCGFloat:(CGFloat)floatValue;
- (instancetype)stringWithCGFloat:(CGFloat)floatValue decimal:(NSUInteger)decimal;
@end
</pre>
<pre>
import "NSString+QMUI.h"
import <CommonCrypto/CommonDigest.h>
import <objc/runtime.h>
define MD5_CHAR_TO_STRING_16 [NSString stringWithFormat: \
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]] \
@implementation NSString (QMUI)
-
(BOOL)includesString:(NSString *)string {
if (!string || string.length <= 0) {
return NO;
}if ([self respondsToSelector:@selector(containsString:)]) {
return [self containsString:string];
}return [self rangeOfString:string].location != NSNotFound;
} (NSString *)trim {
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}(NSString *)trimAllWhiteSpace {
return [self stringByReplacingOccurrencesOfString:@"\s" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
}(NSString *)trimLineBreakCharacter {
return [self stringByReplacingOccurrencesOfString:@"[\r\n]" withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
}(NSString *)md5 {
const char *cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
return MD5_CHAR_TO_STRING_16;
}
-
(NSString *)hexLetterStringWithInteger:(NSInteger)integer {
NSAssert(integer < 16, @"要转换的数必须是16进制里的个位数,也即小于16,但你传给我是%@", @(integer));NSString *letter = nil;
switch (integer) {
case 10:
letter = @"A";
break;
case 11:
letter = @"B";
break;
case 12:
letter = @"C";
break;
case 13:
letter = @"D";
break;
case 14:
letter = @"E";
break;
case 15:
letter = @"F";
break;
default:
letter = [[NSString alloc]initWithFormat:@"%@", @(integer)];
break;
}
return letter;
}
-
(NSString *)removeMagicalChar {
if (self.length == 0) {
return self;
}NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\u0300-\u036F]" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, self.length) withTemplate:@""];
return modifiedString;
} (NSUInteger)lengthWhenCountingNonASCIICharacterAsTwo {
NSUInteger characterLength = 0;
char *p = (char )[self cStringUsingEncoding:NSUnicodeStringEncoding];
for (NSInteger i = 0, l = [self lengthOfBytesUsingEncoding:NSUnicodeStringEncoding]; i < l; i++) {
if (p) {
characterLength++;
}
p++;
}
return characterLength;
}(NSUInteger)transformIndexToDefaultModeWithIndex:(NSUInteger)index {
CGFloat strlength = 0.f;
NSInteger i = 0;
for (i = 0; i < self.length; i++) {
unichar character = [self characterAtIndex:i];
if (isascii(character)) {
strlength += 1;
} else {
strlength += 2;
}
if (strlength >= index + 1) return i;
}
return 0;
}-
(NSRange)transformRangeToDefaultModeWithRange:(NSRange)range {
CGFloat strlength = 0.f;
NSRange resultRange = NSMakeRange(NSNotFound, 0);
NSInteger i = 0;
for (i = 0; i < self.length; i++) {
unichar character = [self characterAtIndex:i];
if (isascii(character)) {
strlength += 1;
} else {
strlength += 2;
}
if (strlength >= range.location + 1) {
if (resultRange.location == NSNotFound) {
resultRange.location = i;
}if (range.length > 0 && strlength >= NSMaxRange(range)) { resultRange.length = i - resultRange.location + (strlength == NSMaxRange(range) ? 1 : 0); return resultRange; } }
}
return resultRange;
} (NSString *)substringAvoidBreakingUpCharacterSequencesFromIndex:(NSUInteger)index lessValue:(BOOL)lessValue countingNonASCIICharacterAsTwo:(BOOL)countingNonASCIICharacterAsTwo {
index = countingNonASCIICharacterAsTwo ? [self transformIndexToDefaultModeWithIndex:index] : index;
NSRange range = [self rangeOfComposedCharacterSequenceAtIndex:index];
return [self substringFromIndex:lessValue ? NSMaxRange(range) : range.location];
}(NSString *)substringAvoidBreakingUpCharacterSequencesFromIndex:(NSUInteger)index {
return [self substringAvoidBreakingUpCharacterSequencesFromIndex:index lessValue:YES countingNonASCIICharacterAsTwo:NO];
}(NSString *)substringAvoidBreakingUpCharacterSequencesToIndex:(NSUInteger)index lessValue:(BOOL)lessValue countingNonASCIICharacterAsTwo:(BOOL)countingNonASCIICharacterAsTwo {
index = countingNonASCIICharacterAsTwo ? [self transformIndexToDefaultModeWithIndex:index] : index;
NSRange range = [self rangeOfComposedCharacterSequenceAtIndex:index];
return [self substringToIndex:lessValue ? range.location : NSMaxRange(range)];
}(NSString *)substringAvoidBreakingUpCharacterSequencesToIndex:(NSUInteger)index {
return [self substringAvoidBreakingUpCharacterSequencesToIndex:index lessValue:YES countingNonASCIICharacterAsTwo:NO];
}(NSString *)substringAvoidBreakingUpCharacterSequencesWithRange:(NSRange)range lessValue:(BOOL)lessValue countingNonASCIICharacterAsTwo:(BOOL)countingNonASCIICharacterAsTwo {
range = countingNonASCIICharacterAsTwo ? [self transformRangeToDefaultModeWithRange:range] : range;
NSRange characterSequencesRange = lessValue ? [self downRoundRangeOfComposedCharacterSequencesForRange:range] : [self rangeOfComposedCharacterSequencesForRange:range];
NSString *resultString = [self substringWithRange:characterSequencesRange];
return resultString;
}(NSString *)substringAvoidBreakingUpCharacterSequencesWithRange:(NSRange)range {
return [self substringAvoidBreakingUpCharacterSequencesWithRange:range lessValue:YES countingNonASCIICharacterAsTwo:NO];
}-
(NSRange)downRoundRangeOfComposedCharacterSequencesForRange:(NSRange)range {
if (range.length == 0) {
return range;
}NSRange resultRange = [self rangeOfComposedCharacterSequencesForRange:range];
if (NSMaxRange(resultRange) > NSMaxRange(range)) {
return [self downRoundRangeOfComposedCharacterSequencesForRange:NSMakeRange(range.location, range.length - 1)];
}
return resultRange;
} (NSString *)stringByRemoveCharacterAtIndex:(NSUInteger)index {
NSRange rangeForRemove = [self rangeOfComposedCharacterSequenceAtIndex:index];
NSString *resultString = [self stringByReplacingCharactersInRange:rangeForRemove withString:@""];
return resultString;
}(NSString *)stringByRemoveLastCharacter {
return [self stringByRemoveCharacterAtIndex:self.length - 1];
}
-
(NSString *)hexStringWithInteger:(NSInteger)integer {
NSString *hexString = @"";
NSInteger remainder = 0;
for (NSInteger i = 0; i < 9; i++) {
remainder = integer % 16;
integer = integer / 16;
NSString *letter = [self hexLetterStringWithInteger:remainder];
hexString = [letter stringByAppendingString:hexString];
if (integer == 0) {
break;
}}
return hexString;
}
@end
@implementation NSString (QMUI_StringFormat)
(instancetype)stringWithNSInteger:(NSInteger)integerValue {
return [NSString stringWithFormat:@"%@", @(integerValue)];
}(instancetype)stringWithCGFloat:(CGFloat)floatValue {
return [NSString stringWithCGFloat:floatValue decimal:2];
}(instancetype)stringWithCGFloat:(CGFloat)floatValue decimal:(NSUInteger)decimal {
NSString *formatString = [NSString stringWithFormat:@"%%.%@f", @(decimal)];
return [NSString stringWithFormat:formatString, floatValue];
}
@end
</pre>