1

i was trying to insert a string into database having multiple value using componentsSeparatedByString:@",". I am getting all the values in "arrayp1net" but problem is how to make a string that contains all the values of array "arrayp1net" separated by comma (,)?

if(p1h < 18)
{
    for(int k=0;k<[arrayp1 count];k++)
    {
        if([[hcar1 objectAtIndex:1]intValue] >= [[ar33 objectAtIndex:k] intValue])
        {
            NSString *str = [NSString stringWithFormat:@"%d",[[[arrayp1 objectAtIndex:k] text] intValue] -1];
            [arrayp1net addObject:str];        
           }
        else
        {
            NSString *str = [NSString stringWithFormat:@"%d",[[[arrayp1 objectAtIndex:k] text]intValue]];
            [arrayp1net addObject:str];
            player1netscore = [NSString stringWithFormat:@"%@",arrayp1net];
        }
    }

yes i need the same but i have to insert in database in one row of a column with query, so that when i am going to fetch, i fetch all as it is.

query=[NSString stringWithFormat:@"insert into normalscoring  
(gameid,coursename,p1,p2,p3,p4,p1s,p2s,p3s,p4s,gameDate,p1nets,p2nets,p3nets,p4nets)  
 values (\'%@\',\'%@\',\'%@||%@\',\'%@||%@\',\'%@||%@\',\'%@||%@\',
 ',,,,,,,,,,,,,,,,,||0||0||0||0',
 ',,,,,,,,,,,,,,,,,||0||0||0||0',
 ',,,,,,,,,,,,,,,,,||0||0||0||0',
 ',,,,,,,,,,,,,,,,,||0||0||0||0',
  \'%@\',',,,,,,,,,,,,,,,,,',
 ',,,,,,,,,,,,,,,,,',',,,,,,,,,,,,,,,,,',',,,,,,,,,,,,,,,,,')",
 txtgameid.text,txtcoursename.text,txtplayer1.text,player1handicap,
 txtplayer2.text,player2handicap,txtplayer3.text,player3handicap,
txtplayer4.text,player4handicap,txtdate.text];

here is what i am trying to do after if statement but getting exception

player1netscore = [NSString stringWithFormat:@"%@",arrayp1net];
arn11=[player1netscore componentsSeparatedByString:@","];
player1netscore=[[arn11 objectAtIndex:0] objectForKey:@"p1nets"];
NSLog(@"player1netscore...%@",player1netscore);
Ketan Shinde
  • 1,847
  • 4
  • 18
  • 38

2 Answers2

5

Use -[NSArray componentsJoinedByString:]:

NSString *stringp1net = [arrayp1net componentsJoinedByString:@","];
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • do you know how to do it in android.? if not known then can you refer to any android developer so that i can ask about it. – Ketan Shinde Nov 30 '11 at 06:46
  • I know how to search stackoverflow... http://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array-elements-with-a-separator-the-oposite-of-spl – rob mayoff Nov 30 '11 at 07:02
0

I am not really clear about what you want.....but whatever I got from your question is you want a string which will contain the elements of arrayp1net array which will be comma seprated

     NSString *str = @"";    
   for(int i = 0; i< [arrayp1net count]; i++){

          str = [str stringByAppendingString:[NSString stringWithFormat:@"%@,",[arrayp1net objectAtIndex:i]]];

      }

For this your arrayp1net should be NSMutable array.

Minakshi
  • 1,437
  • 1
  • 11
  • 19