How to realize the function of intercepting Chinese characters in string by iOS
Editor to share with you how iOS intercepts Chinese characters in strings. I hope you will get something after reading this article. Let's discuss it together.
The details are as follows
Write a simple example to intercept a string from the first Chinese character. Paste the code below:
# import "ViewController.h" @ interface ViewController () @ end@implementation ViewController- (void) viewDidLoad {[super viewDidLoad]; NSArray * array = @ [@ "03-151apple", @ "412banana", @ "# 7tea 15 oranges"]; for (int I = 0; I
< array.count; i++) { NSString *chineseStr = [self getChineseStringWithString:array[i]]; NSLog(@"chineseStr = %@\n", chineseStr); }}- (NSString *)getChineseStringWithString:(NSString *)string{ //(unicode中文编码范围是0x4e00~0x9fa5) for (int i = 0; i < string.length; i++) { int utfCode = 0; void *buffer = &utfCode; NSRange range = NSMakeRange(i, 1); BOOL b = [string getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL]; if (b && (utfCode >= 0x4e00 & & utfCode