我的数组就像这样
(1,2,3,)
(4,5,6,)
(7,8,9,)
我想连接所有这些值,即123456789
我试过这个NSString *str5=[array componentsJoinedByString:@" "];
但我没有得到输出。
任何人都可以向我提供一些信息。
答案 0 :(得分:3)
您的NSArray
似乎有NSArray
。首先循环原始NSArray
并展平它,然后应用componentsJoinedByString
。
创建NSMutableArray
,遍历原始数组并使用每个子数组调用addObjectsFromArray:
。
NSMutableArray *flattenArray = [[NSMutableArray alloc] init];
for(NSArray *array in originalArray)
{
[flattenArray addObjectsFromArray: array];
}
NSString *str5 = [flattenArray componentsJoinedByString:@" "];
这应该有用。
答案 1 :(得分: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);
答案 2 :(得分: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);
我已经取代“(”,“)”,“”&amp;带有“”(null)值的“,”
希望它会对你有所帮助。
谢谢。