Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of intercepting Chinese characters in string by iOS

Shulou Source: shulou.com Published: 2022-06-03 09:36:08 11月15日 Update

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

Tags: Characters strings Chinese characters functions oranges articles apples bananas previous generations examples content finished more knowledge results coding scope industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Redmi Linux Shulou Technology