I have a complex question which may be very simple for you guyz.
I have a
NSMutableString = "This is spinach";
I have 3 Booleans. Please ignore the code. I havent used proper syntax,functions.
if (boolA) appendstring"and apples";
if (boolB) appendstring"and mangoes";
if (boolC) appendstring"and oranges";
every boolean corresponds to a fruit. Say if BOOL a is true, I also have mangoes!
Anyway, my goal is to format the initial String by appending the other fruits if the corresponding booleans are true and present in a very specific way.
Heres the scenario if BOOL A is true, the string should be "This is spinach and mangoes" Note the added "and" with mangoes. I can add it straight away with apending at the end.
But the problem is when all the BOOLeans are true. The string will look like "This is spinach and apples and mangoes and oranges" and this i can achieve.
I want to make it look like "This is spinach And apples, mangoes, oranges" Notice that I want to add "And" only once immediately after and notice the commas "," before mangoes and oranges.
I can get this probably, but with alot of checks and ALOt of unnecessary if/else statements.
I was thinking that I could jus turn
"This is spinach and apples and mangoes and oranges" into
"This is spinach and apples , mangoes , oranges".
im guessing there is some kind of function i can use to do this. Like replacing the second and the third "and" with ",".
Could you guys please lemme know of a function like this and an example to do it. This was possible i think in CPP back in the day.. using strpos() to get location of a char and then do something after a certain number and replace string. Or let me know if theres a better way..
This is for X-code, iphone.