Objective-C - NSArray 到具有多个分隔符的 NSString
Objective-C - NSArray to NSString with multiple delimiters
我有一个 NSArray 字符串,我想将其放入单个 NSString
中,并在 objective-c
.
中用多个分隔符分隔元素
例如数组看起来像这样。
["Mike", "Brother", "Sarah", "Sister", "Jane", "Sister"]
我希望它们在字符串中看起来像这样。
"Mike,Brother; Sarah,Sister; Jane,Sister"
即人和关系用逗号分隔,兄弟姐妹用分号分隔。
使用 appendFormat
的 for
循环将起作用:
BOOL first=YES;
NSMutableString *res = [NSMutableString string];
for (int i = 0 ; i+1 < array.length ; i+=2) {
if (!first) {
[res appendString:@"; "];
} else {
first = NO;
}
[res appendFormat:@"%@,%@", array[i], array[i+1]];
}
使用NSMutableString
有助于避免创建临时对象。
当然这并不理想,因为数组中的项是相关的。您最好创建可以存储名称和关系的对象,这样您就不必将它们存储在平面数组的相邻元素中。
NSArray *input = [ @"Mike", @"Brother", @"Sarah", @"Sister", @"Jane", @"Sister" ];
NSMutableArray *nameSiblingPairs = [NSMutableArray array];
for (NSInteger i = 0; i < [input count]; i += 2)
[nameSiblingPairs addObject:[NSString stringWithFormat:@"%@,%@", input[i], input[i + 1]]];
NSString *output = [nameSiblingPairs componentsJoinedByString:@"; "];
NSArray *array = @[@"Mike", @"Brother", @"Sarah", @"Sister", @"Jane", @"Sister"];
NSMutableArray* newArray = [[NSMutableArray alloc] init];
for (int i = 1; i<[array count]; i+=2){
[newArray addObject: [NSString stringWithFormat:@"%@,%@", array[i-1],array[i]]];
}
NSString* s = [newArray componentsJoinedByString:@"; "];
试试这个:
- (NSString *)serialize:(NSArray *)array
{
NSUInteger count = [array count];
NSAssert(count % 2 == 0, @"Array count must be even");
NSString *result = @"";
NSUInteger i = 0;
for (i = 0; i < count / 2 + 2 ; i+=2) {
NSString *temp = [NSString stringWithFormat:@"%@,%@",array[i], array[i+1]];
result = [result stringByAppendingString:temp];
if (i < count / 2) {
result = [result stringByAppendingString:@"; "];
}
}
return result;
}
这是一种更更好并且干净的方法
NSArray *input = [ @"Mike", @"Brother", @"Sarah", @"Sister", @"Jane", @"Sister" ];
NSMutableString *outputString = [NSMutableString string];
NSArray *delimiters = [@",", @"; "];
for(int i=0; i < outputString.count; ++ i) {
[outputString appendString:[input objectAtIndex:i]];
if (i < input.count - 1) {
[outputString appendString:i % delimiters.count];
}
}
取回数组
NSArray *array = [outputString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@",; "]];
这是一个动态代码,您可以在其中添加 n
个分隔符
试试这个:
NSArray+Extension.h
@interface NSArray (Extension)
- (NSString *)componentsJoinedByStrings:(NSArray *)separatorStrings;
@end
NSArray+Extension.m
@implementation NSArray (Extension)
- (NSString *)componentsJoinedByStrings:(NSArray *)separatorStrings {
NSMutableString *newString = [NSMutableString stringWithString:@""];
for (NSInteger i = 0; i < [self count]; i += 1){
NSMutableString *tempString = [NSString stringWithFormat:@"%@ %@", self[i], separatorStrings[i% [separatorStrings count]]];
[newString appendString:tempString];
}
return newString
}
@end
我有一个 NSArray 字符串,我想将其放入单个 NSString
中,并在 objective-c
.
例如数组看起来像这样。
["Mike", "Brother", "Sarah", "Sister", "Jane", "Sister"]
我希望它们在字符串中看起来像这样。
"Mike,Brother; Sarah,Sister; Jane,Sister"
即人和关系用逗号分隔,兄弟姐妹用分号分隔。
使用 appendFormat
的 for
循环将起作用:
BOOL first=YES;
NSMutableString *res = [NSMutableString string];
for (int i = 0 ; i+1 < array.length ; i+=2) {
if (!first) {
[res appendString:@"; "];
} else {
first = NO;
}
[res appendFormat:@"%@,%@", array[i], array[i+1]];
}
使用NSMutableString
有助于避免创建临时对象。
当然这并不理想,因为数组中的项是相关的。您最好创建可以存储名称和关系的对象,这样您就不必将它们存储在平面数组的相邻元素中。
NSArray *input = [ @"Mike", @"Brother", @"Sarah", @"Sister", @"Jane", @"Sister" ];
NSMutableArray *nameSiblingPairs = [NSMutableArray array];
for (NSInteger i = 0; i < [input count]; i += 2)
[nameSiblingPairs addObject:[NSString stringWithFormat:@"%@,%@", input[i], input[i + 1]]];
NSString *output = [nameSiblingPairs componentsJoinedByString:@"; "];
NSArray *array = @[@"Mike", @"Brother", @"Sarah", @"Sister", @"Jane", @"Sister"];
NSMutableArray* newArray = [[NSMutableArray alloc] init];
for (int i = 1; i<[array count]; i+=2){
[newArray addObject: [NSString stringWithFormat:@"%@,%@", array[i-1],array[i]]];
}
NSString* s = [newArray componentsJoinedByString:@"; "];
试试这个:
- (NSString *)serialize:(NSArray *)array
{
NSUInteger count = [array count];
NSAssert(count % 2 == 0, @"Array count must be even");
NSString *result = @"";
NSUInteger i = 0;
for (i = 0; i < count / 2 + 2 ; i+=2) {
NSString *temp = [NSString stringWithFormat:@"%@,%@",array[i], array[i+1]];
result = [result stringByAppendingString:temp];
if (i < count / 2) {
result = [result stringByAppendingString:@"; "];
}
}
return result;
}
这是一种更更好并且干净的方法
NSArray *input = [ @"Mike", @"Brother", @"Sarah", @"Sister", @"Jane", @"Sister" ];
NSMutableString *outputString = [NSMutableString string];
NSArray *delimiters = [@",", @"; "];
for(int i=0; i < outputString.count; ++ i) {
[outputString appendString:[input objectAtIndex:i]];
if (i < input.count - 1) {
[outputString appendString:i % delimiters.count];
}
}
取回数组
NSArray *array = [outputString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@",; "]];
这是一个动态代码,您可以在其中添加 n
个分隔符
试试这个:
NSArray+Extension.h
@interface NSArray (Extension)
- (NSString *)componentsJoinedByStrings:(NSArray *)separatorStrings;
@end
NSArray+Extension.m
@implementation NSArray (Extension)
- (NSString *)componentsJoinedByStrings:(NSArray *)separatorStrings {
NSMutableString *newString = [NSMutableString stringWithString:@""];
for (NSInteger i = 0; i < [self count]; i += 1){
NSMutableString *tempString = [NSString stringWithFormat:@"%@ %@", self[i], separatorStrings[i% [separatorStrings count]]];
[newString appendString:tempString];
}
return newString
}
@end