Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

objective c - Replace letters in a string

Hello i have a problem that i don't know how to solve. The problem is that I even don't know how to google about it. I've already spent hours on the internet finding the solution, but didn't succeeded. The situation is like this:

I have String, lets say:

NSString *string = @"a?bc?de??fghi?jz?";

my result has to be like this:

NSString *string = @"aabccdeeefghiujzz";

So if you understood i have to do this:

replace ? with a

replace ? with c

replace ? with e

replace ? with e

and so on..

Maybe anyone could help me? i have an answer but it's not very optimized, i am hoping for some more convenient solution.

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let the frameworks do the conversion for you:

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);

This logs:

new: aabccdeeefghiijzz

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...