字符串大小写转换
NSString *testString = @"Hello World";
//大写转化为小写
NSString *lowerCaseString1 = [testString lowercaseString];
//小写转化为大写
NSString *upperCaseString2 = lowerCaseString1.uppercaseString;
字符串中去除空格
NSString *text = [myStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet];
** 获取包含中文的字符串长度**
方法一:
- (int)convertToInt:(NSString*)strtemp
{
int strlength = 0;
char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
方法二:
- (NSInteger)getToInt:(NSString*)strtemp
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData* da = [strtemp dataUsingEncoding:enc];
return [da length];
}
**是否含有中文字符串正则判断
//判断是否是纯汉字
- (BOOL)isChinese
{
NSString *match = @"(^[\u4e00-\u9fa5]+$)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
return [predicate evaluateWithObject:self];
}
//判断是否含有汉字
- (BOOL)includeChinese
{
for(int i=0; i< [self length];i++)
{
int a =[self characterAtIndex:i];
if( a >0x4e00&& a <0x9fff){
return YES;
}
}
抽取字符串中中文字符串
//中文字符数组
NSArray *firstArray = [self getAStringOfChineseWord:str];
//中文顺序字符串
NSString *chineseString2 = [firstArray componentsJoinedByString:@"”];
#pragma mark -获取一段字符串中的中文字
+ (NSArray *)getAStringOfChineseWord:(NSString *)string
{
if (string == nil || [string isEqual:@""]) {
return nil;
}
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (int i=0; i<[string length]; i++) {
int a = [string characterAtIndex:i];
if (a < 0x9fff && a > 0x4e00) {
[arr addObject:[string substringWithRange:NSMakeRange(i, 1)]];
}
}
return arr;
}
这是我在开发中遇到的关于字符串设置处理的一些情况,分享给大家,后续遇到新的还会持续更新,希望与大家交流学习。
网友评论