You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.5 KiB
98 lines
3.5 KiB
// |
|
// NSString+SelectString.m |
|
// FireBoltt |
|
// |
|
// Created by ecell on 2022/11/1. |
|
// Copyright © 2022 Sheldon. All rights reserved. |
|
// |
|
|
|
#import "NSString+SelectString.h" |
|
|
|
@implementation NSString (SelectString) |
|
|
|
- (NSString *)selcteStringWithSelectSatrt:(NSString *)startStr selecteEnd:(NSString *)endStr |
|
{ |
|
NSString *startRegStr = [startStr stringByReplacingOccurrencesOfString:@"[" withString:@"\\["];//转义[ |
|
NSString *endRegStr = [endStr stringByReplacingOccurrencesOfString:@"[" withString:@"\\["];//转义[ |
|
NSString *regStr = [NSString stringWithFormat:@"%@.*?%@",startRegStr,endRegStr];//拼接正则字符 |
|
|
|
if (endStr.length == 0 ) |
|
{ |
|
regStr = [NSString stringWithFormat:@"%@.*",startRegStr]; |
|
} |
|
|
|
if (startStr.length == 0) |
|
{ |
|
regStr = [NSString stringWithFormat:@".*?%@",endStr]; |
|
} |
|
|
|
//正则方法截取字符串 |
|
NSRange codeRange = [self rangeOfString:regStr options:NSRegularExpressionSearch]; |
|
NSString *return_code; |
|
if (codeRange.location != NSNotFound) |
|
{ |
|
return_code = [self substringWithRange:codeRange]; |
|
if (startStr.length) |
|
{ |
|
return_code = [return_code stringByReplacingOccurrencesOfString:startStr withString:@""]; |
|
} |
|
if (endStr.length) |
|
{ |
|
return_code = [return_code stringByReplacingOccurrencesOfString:endStr withString:@""]; |
|
} |
|
} |
|
else |
|
{ |
|
return_code = @"字符串截取失败"; |
|
} |
|
return return_code; |
|
} |
|
|
|
+ (NSString *)replaceUnicode:(NSString *)unicodeStr |
|
{ |
|
NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"]; |
|
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""]; |
|
NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""]; |
|
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding]; |
|
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData |
|
mutabilityOption:NSPropertyListImmutable |
|
format:NULL |
|
errorDescription:NULL]; |
|
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"]; |
|
} |
|
|
|
+(NSString *)utf8ToUnicode:(NSString *)string |
|
{ |
|
NSUInteger length = [string length]; |
|
NSMutableString *str = [NSMutableString stringWithCapacity:0]; |
|
for (int i = 0;i < length; i++){ |
|
NSMutableString *s = [NSMutableString stringWithCapacity:0]; |
|
unichar _char = [string characterAtIndex:i]; |
|
// 判断是否为英文和数字 |
|
if (_char <= '9' && _char >='0'){ |
|
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]]; |
|
}else if(_char >='a' && _char <= 'z'){ |
|
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]]; |
|
}else if(_char >='A' && _char <= 'Z') |
|
{ |
|
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]]; |
|
}else |
|
{ |
|
// 中文和字符 |
|
[s appendFormat:@"\\u%x",[string characterAtIndex:i]]; |
|
// 不足位数补0 否则解码不成功 |
|
if(s.length == 4) |
|
{ |
|
[s insertString:@"00" atIndex:2]; |
|
} |
|
else if (s.length == 5) |
|
{ |
|
[s insertString:@"0" atIndex:2]; |
|
} |
|
} |
|
[str appendFormat:@"%@", s]; |
|
} |
|
return str; |
|
} |
|
|
|
@end
|
|
|