如何将数组中的多个值从逗号分隔为字符串

时间:2011-11-17 06:05:02

标签: iphone ios

我试图使用componentsSeparatedByString:@“,”将字符串插入到具有多个值的数据库中。我得到了“arrayp1net”中的所有值,但问题是如何创建一个字符串,其中包含以逗号(,)分隔的数组“arrayp1net”的所有值?

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];
        }
    }

是的我需要相同但我必须在数据库的一行中插入带有查询的数据库,这样当我要获取时,我会获取所有内容。

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

这是我在if语句之后尝试做但是得到异常

player1netscore = [NSString stringWithFormat:@"%@",arrayp1net];
arn11=[player1netscore componentsSeparatedByString:@","];
player1netscore=[[arn11 objectAtIndex:0] objectForKey:@"p1nets"];
NSLog(@"player1netscore...%@",player1netscore);

2 个答案:

答案 0 :(得分:5)

使用-[NSArray componentsJoinedByString:]

NSString *stringp1net = [arrayp1net componentsJoinedByString:@","];

答案 1 :(得分:0)

我并不是很清楚你想要什么.....但是我从你的问题中得到的是你想要一个包含arrayp1net数组元素的字符串,它将以逗号分隔

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

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

      }

为此你的arrayp1net应该是NSMutable数组。