nsstring遍历汉子

时间:2024-05-22 09:37:14
NSString *mytimestr=@"好人一生平安";
size_t length = [mytimestr length];
for (size_t i=; i < length; i++) {
unichar c = [mytimestr characterAtIndex:i];
NSLog(@"%C", c);
}

uitextfield获取中文:

      [self.tf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

- (void)textFieldDidChange:(UITextField *)tf {
NSMutableString *str = [NSMutableString string];
if (tf.text.length > 0) {
for (int i = 0; i<tf.text.length; i++) {
unichar c = [tf.text characterAtIndex:i];
if (c >=0x4E00 && c <=0x9FA5)
{
[str appendString:[tf.text substringWithRange:NSMakeRange(i, 1) ]];
}
} }
NSLog(@"-------:%@",str); }