你好,我有,我不知道如何解决问题。 问题是,我甚至不知道如何google一下吧。 我已经花了在互联网上寻找时间的解决方案,但没有成功。 情况是这样的:
我有字符串,可以说:
NSString *string = @"aąbcčdeęėfghiįjzž";
我的成绩是这样的:
NSString *string = @"aabccdeeefghiujzz";
所以,如果你明白我必须这样做:
与更换
替换C C
随e代替E.
随e代替E.
等等..
也许有人可以帮助我吗? 我有一个答案,但它不是很优化,我希望更方便一些解决方案。
提前致谢!
让框架为你做的转换:
NSString *blah = @"aąbcčdeęėfghiįjzž";
NSData *data = [blah dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *newblah = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"new: %@", newblah);
该日志:
new: aabccdeeefghiijzz
这将工作:
NSString *str = @"aąbcčdeęėfghiįjzž";
NSLog(@"%@", str);
NSMutableString *s = [[str decomposedStringWithCompatibilityMapping] mutableCopy];
NSUInteger pos = 0;
while(pos < s.length) {
NSRange r = [s rangeOfComposedCharacterSequenceAtIndex:pos];
if (r.location == NSNotFound) break;
pos = ++r.location;
if (r.length == 1) continue;
r.length--;
[s deleteCharactersInRange:r];
}
NSLog(@"%@", s);
我们的想法是用变音标志每个字符先分解成其基本字符的序列,序列的Unicode组合语音标记 (这是通过做decomposedStringWithCompatibilityMapping
),然后再通过串,并删除所有此类商标一一(那是什么循环一样)。
我只能提供这样的:
[string stringByReplacingOccurrencesOfString:@"ą" withString:@"a"];
等等...
戴夫·德隆的答案将取代 “AE” 与 “AE” 和 “SS” 与 “S”,但不会取代œ连写,ij,FF,音响,佛罗里达州,FFI,ffl第,FT,ST,...
一种改进的解决方案是增加三行映射来处理一切正常:
blah = [blah stringByReplacingOccurrencesOfString:@"Œ" withString:@"OE"];
blah = [blah stringByReplacingOccurrencesOfString:@"œ" withString:@"oe"];
blah = [blah precomposedStringWithCompatibilityMapping];
NSData *data = [blah dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *newblah = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];