amit srivastava
amit srivastava

Reputation: 77

How to convert the array's string value

I have the array that is save following values:

globalarray_beacon: (
"(01000466)",
"(01000008)",
"(01000003)",
"(01000001)",
"(01000006)",
"(01000004)",
"(01000007)",
"(01000005)",
"(01000469)",
"(01000468)",
"(01000420)",
"(01000002)",
"(01000444)",
"(01000463)",
"(01000468)",
"(01000466)",
"(01000001)",
)

I want to convert this values into following:

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:@"()"]];

}

how to convert the array's value I have to tried many things. How's that possible to convert string value to data. Thanks in advance.

Upvotes: 1

Views: 143

Answers (5)

holex
holex

Reputation: 24041

I would go for a category first, that'd help you out instantly, even if you don't want to spent resources to convert your entire array (for reasons).


it'd look like this:

#import <Foundation/Foundation.h>

@interface NSString (MyTrimmedString)

- (NSString *)trimmed;

@end

and, of course, the essential implementation:

@implementation NSString (MyTrimmedString)

- (NSString *)trimmed {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"()"]];
}

@end

then you can directly apply it on strings, like e.g.:

NSString *_thingy = @"(01000466)".trimmed; // that will present "01000466"

or even on your array's items, like e.g.:

NSArray *_array = [NSArray arrayWithObjects:@"(01000466)", @"(01000008)", @"(01000003)", @"(01000001)", @"(01000006)", nil];
NSString *_thingy = [_array firstObject].trimmed; // that will present "01000466"

from here, creating a new array (if necessary) is a piece of cake:

NSMutableArray *_newArray = [NSMutableArray array];
for (NSString * obj in _array) {
    [_newArray addObject:obj.trimmed];
}

which gives you an array with the trimmed elements, like e.g.:

// ( "01000466", "01000008", "01000003", "01000001", "01000006" )

Upvotes: 0

Prissy Eve
Prissy Eve

Reputation: 156

Try this below code

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

Upvotes: 1

vadian
vadian

Reputation: 285082

NSString has a convenience method stringByTrimmingCharactersInSet to do that

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

Upvotes: 5

KKRocks
KKRocks

Reputation: 8322

Try this :

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

Upvotes: 1

karthikeyan
karthikeyan

Reputation: 3888

Try this

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

Upvotes: 0

Related Questions