开发者

searching and replacing an extended ASCII in a string

a noob question here.

I am trying to make an automatic search and replace process for characters' ASCII values in a string.

so, I have a string constructed from a content of a UITextField

NSString *searchText;
searchText = (mmText.text);

then I do a little loop and check all entered characters for their ASCII values. if they're not in the allowed range I want to search and replace them with something else (? for now)

so let's say I am in the loop and I get to a ASCII 45 character (it's a minus sign):

int asciiCode = 45;

now I would like to find the ASCII 45 character in the string and replace it with a question mark

This is what I am doing at the moment:

 NSString *ascStr = [NSString stringWithFormat:@"%c", asciiCode];
 NSRange matchSpace;
 matchSpace = [searchText rangeOfString: ascStr];
 if (matchSpace.location == NSNotFound)
 {}
 else   
 NSMutableString *searchandReplace = [NSMutableString stringWithString: searchText];
 [searchandReplace replaceCharactersInRange: [searchandReplace rangeOfString: ascStr] withString: @"?"];
  mmText.text = searchandReplace;
  }

This works fine for a regular ASCII value (0-255), but it doesn't seem to work for the extended ASCII values coming from foreign languages. For example when using the Korean language mode, one of the main character looks like a double crossed 开发者_如何学编程W, but when printed via NSLog it looks like a copyright sign. This is probably the reason the search and replace procedure doesn't work for it. It has an ASCII value of 8361.

any ideas ? thank you!


it turns out it was as simple as changing:

NSString *ascStr = [NSString stringWithFormat:@"%c", asciiCode];

to

NSString *ascStr = [NSString stringWithFormat:@"%C", asciiCode];

%c 8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \ddd or the Unicode hexadecimal format \udddd, where d is a digit

%C 16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \ddd or the Unicode hexadecimal format \udddd, where d is a digit

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜