我有保存以下值的数组:
globalarray_beacon: (
"(01000466)",
"(01000008)",
"(01000003)",
"(01000001)",
"(01000006)",
"(01000004)",
"(01000007)",
"(01000005)",
"(01000469)",
"(01000468)",
"(01000420)",
"(01000002)",
"(01000444)",
"(01000463)",
"(01000468)",
"(01000466)",
"(01000001)",
)
我想将此值转换为以下内容:
globalarray_beacon: (
01000468,
01000004,
01000006,
01000420,
01000444,
01000466,
01000469,
01000003,
01000005,
01000008,
01000001,
01000007,
01000002
)
try1。
NSArray *items = [theString componentsSeparatedByString:@" " "];
try2。
NSString *stringWithoutSpaces = [myString
stringByReplacingOccurrencesOfString:@"()" withString:@""];
try3。
for (int i=0; i<array_lastname.count; i++) {
[array_data addObject:[[array_lastname objectAtIndex:i] componentsSeparatedByString:@"()"]];
}
如何转换数组的值我必须尝试很多东西。如何将字符串值转换为数据。 提前谢谢。
答案 0 :(得分:5)
NSString
有一个方便的方法stringByTrimmingCharactersInSet
来做
NSArray *globalarray_beacon = @[@"(01000466)", @"(01000008)", @"(01000003)", @"(01000001)", @"(01000006)", @"(01000004)", @"(01000007)", @"(01000005)", @"(01000469)", @"(01000468)", @"(01000420)", @"(01000002)", @"(01000444)", @"(01000463)", @"(01000468)", @"(01000466)", @"(01000001)"];
NSMutableArray *trimmedArray = [NSMutableArray array];
for (NSString * string in globalarray_beacon) {
[trimmedArray addObject:[string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"()"]]];
}
NSLog(@"%@", trimmedArray);
答案 1 :(得分:1)
请尝试以下代码
NSString *tempString = [[arrayOfStrings objectAtIndex:i] stringByReplacingOccurrencesOfString:@"(" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@")" withString:@""];
[arrayOfConvertedStrings addObject:tempString];
答案 2 :(得分:1)
试试这个:
NSMutableArray *arr =[[NSMutableArray alloc]initWithObjects:@"(123)",@"(321)", nil];
for (int i = 0; i<arr.count; i++) {
NSString *str = [arr objectAtIndex:i];
str = [str stringByReplacingOccurrencesOfString:@"(" withString:@"" ];
str = [str stringByReplacingOccurrencesOfString:@")" withString:@"" ];
[arr replaceObjectAtIndex:i withObject:str];
}
答案 3 :(得分:0)
试试这个
NSArray *array = [[NSArray alloc]initWithObjects:@"(12345)",@"(12345)", nil];
NSMutableArray *obj = [NSMutableArray array];
for(NSString *str in array){
NSString *temp = [str stringByReplacingOccurrencesOfString:@"(" withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@")" withString:@""];
[obj addObject:temp];
}
答案 4 :(得分:0)
我会首先选择一个类别,即使您不想花费资源转换整个阵列(原因),也会立即帮助您。
它看起来像这样:
#import <Foundation/Foundation.h>
@interface NSString (MyTrimmedString)
- (NSString *)trimmed;
@end
当然还有基本的实施:
@implementation NSString (MyTrimmedString)
- (NSString *)trimmed {
return [self stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"()"]];
}
@end
然后你可以直接将它应用于字符串,例如:
NSString *_thingy = @"(01000466)".trimmed; // that will present "01000466"
甚至是您阵列中的项目,例如:
NSArray *_array = [NSArray arrayWithObjects:@"(01000466)", @"(01000008)", @"(01000003)", @"(01000001)", @"(01000006)", nil];
NSString *_thingy = [_array firstObject].trimmed; // that will present "01000466"
从这里开始,创建一个新数组(如果需要)是件小事:
NSMutableArray *_newArray = [NSMutableArray array];
for (NSString * obj in _array) {
[_newArray addObject:obj.trimmed];
}
它为您提供了一个带有修剪元素的数组,例如:
// ( "01000466", "01000008", "01000003", "01000001", "01000006" )