//
// CCNSString.h
// CCFC
//
#ifndef CC_NSSTRING
#define CC_NSSTRING
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#define PASCAL_STR %.*s
#define LONG_LONG_INT %lld
#define LONG_LONG_UINT %llu
#define UNICHAR %C
@interface NSString(cc)
// 获取字符在字符串第一次出现的位置
- (NSInteger)firstIndexOfChar:(unichar)ch;
// 获取字符在字符串最后一次出现的位置
- (NSInteger)lastIndexOfChar:(unichar)ch;
// 获取字符在字符串第N次出现的位置, the appear count starts with 1.
- (NSInteger)firstIndexOfChar:(unichar)ch withAppearCount:(NSInteger)count;
// it won't modify the str, but returns another string that be trimmed.
- (NSString *)returnTrimmed;
// 根据字符串的字体大小获取字符串的像素长度
- (CGFloat)getStrPixelLenByFontSize:(CGFloat)fontSize;
// 根据字符串的字体大小获取字符串的像素高度
- (CGFloat)getStrPixelHeightByFontSize:(CGFloat)fontSize;
// 获取字符串中字符下标从前到后,总长度小于等于指定像素长度的最大下标的值
- (NSInteger)getMaxIndexLessEqualToLen:(CGFloat)fontSize maxLen:(CGFloat)maxLen;
// print each unichar
- (void)print;
// returns the system library path of the indicate name
- (NSString *)systemLibraryPath;
// returns the empty string
+ (NSString *)emptyString;
// returns the text char count of the string, eg, SMS's character count
- (int)calculateTextCharCount;
// returns the trimmed string that be trimmed not only in the beginning and end, also in the middle of the string
- (NSString *)returnTrimmedEntile;
// returns whether it contains the str
- (BOOL)containsStr:(NSString *)str;
// returns whether the string is in strArr(whether the strArr contains at least one string that is equalToString self)
- (BOOL)stringInArr:(NSArray *)strArr;
@end
@interface NSMutableString(cc)
// 移除字符串首部、尾部、首部和尾部的空白字符(Unicode形式的空格,'\t', '\n')
- (void)trimBegin;
- (void)trimEnd;
- (void)trim;
// same to trim func, but call system function
- (void)trimByCallSystemFunc;
@end
#endif //__OBJC__
#endif //CC_NSSTRING
//
// CCNSString.m
// CCFC
//
//
#include <wchar.h>
#ifdef __OBJC__
#import "CCNSString.h"
#import "CCLog.h"
@implementation NSString(cc)
// 获取字符在字符串第一次出现的位置
- (NSInteger)firstIndexOfChar:(unichar)ch
{
return [self firstIndexOfChar:ch withAppearCount:1];
}
// 获取字符在字符串最后一次出现的位置
- (NSInteger)lastIndexOfChar:(unichar)ch
{
for(NSInteger i = self.length - 1; i >= 0; i--)
{
if(ch == [self characterAtIndex:i])
{
return i;
}
}
return -1; // 没找到
}
// 获取字符在字符串第N次出现的位置
- (NSInteger)firstIndexOfChar:(unichar)ch withAppearCount:(NSInteger)count
{
NSInteger appearCount = 0;
for(NSInteger i = 0; i < self.length; i++)
{
if(ch == [self characterAtIndex:i])
{
appearCount++;
if(appearCount == count)
{
return i;
}
}
}
return -1; // 没找到
}
- (NSString *)returnTrimmed
{
NSMutableString *temp = [NSMutableString stringWithString:self];
[temp trim];
return temp;
}
// 根据字符串的字体大小获取字符串的像素长度
- (CGFloat)getStrPixelLenByFontSize:(CGFloat)fontSize
{
CGSize size = [self sizeWithFont:[UIFont systemFontOfSize:fontSize]];
return size.width;
}
// 根据字符串的字体大小获取字符串的像素高度
- (CGFloat)getStrPixelHeightByFontSize:(CGFloat)fontSize
{
CGSize size = [self sizeWithFont:[UIFont systemFontOfSize:fontSize]];
return size.height;
}
// 获取字符串中字符下标从前到后,总长度小于等于指定像素长度的最大下标的值
- (NSInteger)getMaxIndexLessEqualToLen:(CGFloat)fontSize maxLen:(CGFloat)maxLen
{
CGFloat sumLen = 0;
for(NSInteger i = 0; i < self.length; i++)
{
NSString *temp = [self substringWithRange:NSMakeRange(i, 1)]; // 获取当前1个字符对应的字符串
CGFloat currentCharPixelLen = [temp getStrPixelLenByFontSize:fontSize];
sumLen += (CGFloat)currentCharPixelLen; // 累计当前的总长度
if(sumLen > maxLen)
{
return i - 1;
}
else if(sumLen == maxLen)
{
return i;
}
else
{
continue;
}
}
return self.length - 1;
}
// print each unichar
- (void)print
{
for(int i = 0; i < [self length]; ++i)
NSLog(@"%C", [self characterAtIndex:i]);
}
// returns the system library path of the indicate name
- (NSString *)systemLibraryPath
{
return [NSString stringWithFormat:
@"/System/Library/Frameworks/%@.framework/%@", self, self];
}
// returns the empty string
+ (NSString *)emptyString
{
return @"";
}
// returns the text char count of the string, eg, SMS's character count
- (int)calculateTextCharCount
{
float number = 0.0;
for(int i = 0; i < [self length]; ++i)
{
NSString *str = [self substringWithRange:NSMakeRange(i, 1)];
LOG_STR(str);
if([str lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3)
{
LOG_DOUBLE(number);
number++;
}
else
{
LOG_DOUBLE(number);
number = number + 0.5;
}
}
return ceil(number);
}
// returns the trimmed string that be trimmed not only in the beginning and end, also in the middle of the string
- (NSString *)returnTrimmedEntile
{
NSCharacterSet *charSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
return [self stringByTrimmingCharactersInSet:charSet];
}
// returns whether it contains the str
- (BOOL)containsStr:(NSString *)str
{
return ([self rangeOfString:str].location != NSNotFound);
}
// returns whether the string is in strArr(whether the strArr contains at least one string that is equalToString self)
- (BOOL)stringInArr:(NSArray *)strArr
{
for(NSString *temp in strArr)
if([self isEqualToString:temp])
return TRUE;
return FALSE;
}
@end
@implementation NSMutableString(cc)
- (void)trimBegin
{
int i;
for(i = 0; i < self.length; i++)
{
unichar ch = [self characterAtIndex:i];
if(!iswspace(ch))
{
break;
}
}
if(0 == i)
{
return;
}
[self deleteCharactersInRange:NSMakeRange(0, i)];
}
- (void)trimEnd
{
int i;
for(i = self.length - 1; i >= 0; i--)
{
unichar ch = [self characterAtIndex:i];
if(!iswspace(ch))
{
break; //从后朝前找到不是空白字符的位置
}
}
if(i == self.length - 1)
{
return;
}
NSUInteger loc = i + 1;
[self deleteCharactersInRange:NSMakeRange(loc, self.length - loc)];
}
- (void)trim
{
[self trimBegin];
[self trimEnd];
}
// same to trim func, but call system function
- (void)trimByCallSystemFunc
{
CFStringTrimWhitespace((CFMutableStringRef)self);
}
@end
#endif //__OBJC__
微风不燥,阳光正好,你就像风一样经过这里,愿你停留的片刻温暖舒心。
我是程序员小迷(致力于C、C++、Java、Kotlin、Android、Shell、JavaScript、TypeScript、Python等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。
欢迎关注。助您在编程路上越走越好!