如何转换数组的字符串值

How to convert the array's string value

我有保存以下值的数组:

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
)

尝试1.

NSArray *items = [theString componentsSeparatedByString:@" " "];

尝试2。

 NSString *stringWithoutSpaces = [myString 
  stringByReplacingOccurrencesOfString:@"()" withString:@""];

尝试3.

  for (int i=0; i<array_lastname.count; i++) {


   [array_data addObject:[[array_lastname objectAtIndex:i] componentsSeparatedByString:@"()"]];

}

如何转换数组的值我必须尝试很多东西。怎么可能将字符串值转换为数据。 提前致谢。

试试下面的代码

NSString *tempString = [[arrayOfStrings objectAtIndex:i] stringByReplacingOccurrencesOfString:@"(" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@")" withString:@""];
[arrayOfConvertedStrings addObject:tempString];

试试这个

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];
    }

试试这个:

 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];
    }

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);

我会先选择一个类别,这会立即帮助您,即使您不想花费资源来转换整个数组(出于某些原因)。


看起来像这样:

#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" )