I am trying to create comma separated string. e.g. abc,pqr,xyz
I am parsing开发者_运维技巧 the xml and want to generate the comma separated string of the values of nodes in xml.I am doing the following:
if([elementName isEqualToString:@"Column"])
NSString *strTableColumn = [attributeDict objectForKey:@"value"];
I am getting different nodes value in strTableColumn
while parsing and want to generate comma separated of this. Please help.
I would do to it like this. Before you start your XML processing, create a mutable array to hold each "Column" value (probably want this to be an iVar in your parser class):
NSMutableArray *columns = [[NSMutableArray alloc] init];
Then parse the XML, adding each string to the array:
if([elementName isEqualToString:@"Column"]) {
[columns addObject:[attributeDict objectForKey:@"value"]];
}
When you're done, create the comma-separated string and release the array:
NSString *strTableColumn = [columns componentsJoinedByString:@","];
[columns release];
columns = nil;
You can use the following code:
NSString *timeCreated = elementName;
NSArray *timeArray = [timeCreated componentsSeparatedByString:@","];
NSString *t = [timeArray objectAtIndex:0];
NSString *t1 = [timeArray objectAtindex:1];
Then append one by one string.
use NSMutableString
then you can use
[yourMutableString appendString:@","];//there is a comma
[yourMutableString appendString:someOtherString];
in this way your strings are separated by comma
This method will return you the nsmutablestring with comma separated values from an array
-(NSMutableString *)strMutableFromArray:(NSMutableArray *)arr withSeperater:(NSString *)saperator
{
NSMutableString *strResult = [NSMutableString string];
for (int j=0; j<[arr count]; j++)
{
NSString *strBar = [arr objectAtIndex:j];
[strResult appendString:[NSString stringWithFormat:@"%@",strBar]];
if (j != [arr count]-1)
{
[strResult appendString:[NSString stringWithFormat:@"%@",seperator]];
}
}
return strResult;
}
First thought can be that you parse the data and append commas using the NSString append methods. But that can have extra checks. So better solution is to
Parse data -> Store into array -> Add comma separators -> Finally store in string
// Parse data -> Store into array
NSMutableArray *arrayOfColumns = [[NSMutableArray alloc] init];
if([elementName isEqualToString:@"Column"]){
[arrayOfColumns addObject:[attributeDict objectForKey:@"value"]];
}
// Add comma separators -> Finally store in string
NSString *strTableColumn = [arrayOfColumns componentsJoinedByString:@","];
精彩评论