1

我的数组是这样的

(1,2,3,)
(4,5,6,)
(7,8,9,)

我想连接所有这些值,即;123456789

我试过这样NSString *str5=[array componentsJoinedByString:@" "];

但我没有得到输出。

谁能给我一些信息。

4

3 回答 3

3

看起来你有一个NSArrayof NSArray。首先循环遍历原件NSArray并将其展平,然后应用componentsJoinedByString

创建一个NSMutableArray, 循环遍历您的原始数组并调用addObjectsFromArray:每个子数组。

NSMutableArray *flattenArray = [[NSMutableArray alloc] init];
for(NSArray *array in originalArray)
{
    [flattenArray addObjectsFromArray: array];
}

NSString *str5 = [flattenArray componentsJoinedByString:@" "];

这应该有效。

于 2013-03-14T09:34:18.833 回答
0

像这样尝试它会给出预期的输出,但我不知道它是否有效,

NSArray *arr=[[NSArray alloc]initWithObjects:@"(1,2,3)",@"(4,5,6)", nil];
    NSString *str=[arr componentsJoinedByString:@""];
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@",()"];
    str = [[str componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
    NSLog(@"%@",str);
于 2013-03-14T09:30:53.330 回答
0

试试这个用空值替换括号的字符。

代码 ::

NSArray *arr = [[NSArray alloc] initWithObjects:@"(1, 2, 3, )", @"(4, 5, 6, )", @"(7, 8, 9, )", nil];

for (int i = 0; i < [arr count]; i++) {
    NSLog (@"==> %@", [or objectAtIndex:i]);
}

NSString *str5 = [arr componentsJoinedByString:@" "];
NSLog(@" ==> %@", str5);

str5 = [str5 stringByReplacingOccurrencesOfString:@"(" withString:@""];
str5 = [str5 stringByReplacingOccurrencesOfString:@" " withString:@""];
str5 = [str5 stringByReplacingOccurrencesOfString:@")" withString:@""];
str5 = [str5 stringByReplacingOccurrencesOfString:@"," withString:@""];

//Or Try this
NSCharacterSet *doNotWantChars = [NSCharacterSet characterSetWithCharactersInString:@",() "];
str5 = [[str5 componentsSeparatedByCharactersInSet: doNotWantChars] componentsJoinedByString: @""];
NSLog(@" ==> %@", str5);

NSLog(@"Output :: %@", str5);

我已将"(", ")", "" & ","替换为"" (null) 值

希望它会帮助你。
谢谢。

于 2013-03-14T09:31:36.617 回答