views:

2051

answers:

4

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.

A: 

I would use each bool to append the fruit string to an array.

items = [NSArray new];

if (boolA) [items append: @"apples"];
if (boolB) [items append: @"bannanas"];
if (boolC) [items append: @"cranberries"];

Then I would look at the length of the array and do the 'right' thing for whatever language (English, French, etc.).

pseudo/broken Objc code:

r = [NSMutableString string];
[r appendString: @"This is "];

if ([items length] == 1) {
  [r appendString: [items at: 0]];
}
if ([items length] == 2) {
  [r appendStringWithFormat: @"%@ and %@ ", [items at: 0], [items at: 1]] ;
}
if ([items length] > 2) {
  for (int i = 0; i < [items length]; i++) {    
    if (i == ([items length] - 1)) //if last
      [r appendString: @"and "];
    [r appendString: [items at: i]];
    [r appendString: @", "];
  }
}

Again, this is just objective-pseudo code. I'm just giving you the gist rather than giving you something you should paste into your system. There are probably the wrong method names and mem leaks with this.

Also, if you start dealing with plural and singular, then things get even more complicated, but not by too much.

cheers

drudru
A: 

Well a very simple and naive approach would be:

NSMutableString* message = [NSMutableString stringWithCapacity:15];
[message setString:@"This is spinach"];

BOOL boolA = YES, boolB = NO, boolC = YES;

// add "and" if there are any fruits
if(boolA || boolB || boolC)
    [message appendString:@" and"];

if(boolA)
    [message appendString:@" apples,"];
if(boolB)
    [message appendString:@" oranges,"];
if(boolC)
    [message appendString:@" mangoes,"];

// remove last ","
if(boolA || boolB || boolC)
 [message deleteCharactersInRange:NSMakeRange([message length] - 1, 1)];

This results in

This is spinach and apples, mangoes
Daniel Rinser
A: 

Here's a cleaner solution that uses arrays instead of bool1 bool2 etc.

Do a check to see how many of the bools are true (easier if you put them in an array, put the strings in another array), so you have an idea of what you're output will be. The array of strings should only contain the unique fruits and not "and"s. You're going to have a bool called isand =false; Iterate through the bool array, if you get a true, append " and " + fruit name in other array, and set the other bool to true. Then you know for the rest not to add more "and"s.

edit: that's hard to understand, here's some code (my objective C syntax may not be perfect, or even close to perfect, please correct my mistakes and comment :D )

bool isand=false;
NSMutableArray * strings = [NSMutableArray alloc] init];
NSMutableArray * bools = [NSMutableArray alloc] init];
NSMutableString = @"This is spinach";

//fill arrays with values

for (NSInt x=0; x<[string length]; x++) //could've used for in, but need to know which index to use in other array
{
if (b)
{
if (!isand)
{
[mystring appendString: @" and "];
isand=true;
}
[mystring appendString: [strings objectAtIndex: x];
}
}

I've probably made many horrible errors, but hopefully you get the idea :D

CrazyJugglerDrummer
+1  A: 

Another solution, this time using + [NSArray componentsJoinedByString:]

NSMutableArray* fruits = [NSMutableArray arrayWithCapacity:3];

if(boolA)
    [fruits addObject:@"apples"];
if(boolB)
    [fruits addObject:@"oranges"];
if(boolC)
    [fruits addObject:@"mangoes"];

NSString* message;
if([fruits count] > 0)
 message = [NSString stringWithFormat:@"This is spinach and %@", [fruits componentsJoinedByString:@", "]];
else
 message = @"This is spinach";
Daniel Rinser