views:

217

answers:

1

Hi guys,

I wanted to know if theres a single method or way that will help me replace strings for specific characters.

like MALE - M FEMALE - F CHILD - P

The longer way out is this..

[str stringByreplacingOccurencesOfString:@"MALE" withString:@"M"];
[str stringByreplacingOccurencesOfString:@"FEMALE" withString:@"F"];
[str stringByreplacingOccurencesOfString:@"CHILD" withString:@"P"];

I was wondering if theres another way in which i can reduce lines of code here, specially when there are alots of things to replace.

thanks.

this is for iPhone OS.

+2  A: 

No, but it shouldn't take more than 5-10 minutes to write a method that takes an array or varargs to do it for you.

Azeem.Butt
using an array?? any simple example?
Sam
I've loaded all strings "to be replaced" in an NSDictionary. And I kept doing, [str stringByReplacingOc..@"MALE" withString:[dict objectForKey:@"MALE"]];There are sooo many replacements, any detail is appreciated man.
Sam
I had an idea, where i'd attach '$' in the beginning of the str to be replaced. for example.$Male is for M What Maybe I could do is, look for the $ character and then take that word out and replace it from the Dictionary. Maybe its easier than looping through a long list?I wish I new how to do that..
Sam