3

I have a string in the following format

myString = "cat+dog+cow"

I need to store each string separated by + in to a array. Eg:

myArray[0] = cat
myArray[1] = dog
myArray[2] = cow

Can anyone tell me the proper way to do this?

0

12 Answers 12

15

componentsSeparatedByString: splits the string and return the result in an array.

NSArray *myArray = [myString componentsSeparatedByString:@"+"];

[myArray objectAtIndex:0];//cat
[myArray objectAtIndex:1];//dog
[myArray objectAtIndex:2];//cow
Sign up to request clarification or add additional context in comments.

1 Comment

@Sharman: Just for the sake of completeness: The inverse would be this: NSString *myString = [myArray componentsJoinedByString:@"+"];.
3

Try this..

NSArray *arr = [myString componentsSeparatedByString:@"-"];

[arr objectAtIndex:0];//Hai
[arr objectAtIndex:1];//Welcome

9 Comments

without using array.because if i use array there is a chance to occur error
i have a message and accessmode
if you alloc and release array correctly,then no need to worry about that.
i will append access mode at first and then - and my message..i need to separate access mode and message.if i spitted like an array ther will be a problem if my message contains - character other than i appended
is the length of access mode constant
|
1

It is vert simple..

NSString * test = @"Hello-hi-splitting-for-test";
NSArray * stringArray = [test componentsSeparatedByString:@"-"];

// Now stringArray will contain all splitted strings.. :)

Hope this helps...

I you don't want use array then iterate through each character...

NSMutableString * splittedString = nil;

for(int i=0;i<test.length;i++){

    unichar character = [test characterAtIndex:0];
    if (character=='-') {

        if (splittedString!=nil) {
            NSLog(@"String component %@",splittedString);
            [splittedString release];
            splittedString = nil;
        }

    } else {
        if (splittedString==nil) {
            splittedString = [[NSMutableString alloc] init];                
        }
        [splittedString appendFormat:@"%C",character];
    }

}

if (splittedString!=nil) {
    NSLog(@"String last component %@",splittedString);
    [splittedString release];
    splittedString = nil;
}

Thats all...

1 Comment

i will append access mode at first and then - and my message..i need to separate access mode and message.if i spitted like an array ther will be a problem if my message contains - character other than i appended – Sharmain 0 secs ago edit
1
NSArray *myWords = [myString componentsSeparatedByString:@"+"];

Comments

1

You can find this one very simple

NSString *str = @"cat+dog+cow";
NSArray *arr = [str componentsSeparatedByString:@"+"];
NSLog(@"Array items %@",arr);

OUTPUT:

Array items ( Cat, dog, Cow )

Comments

1

Use the componentsSeparatedByString: method of NSString.

 NSString string = @"hai-welcome";
 NSArray myArray = [string componentsSeparatedByString:@"-"];

NSString* haiString = [myArray objectAtIndex:0];
NSString* welcomeString = [myArray objectAtIndex:1];

1 Comment

i will append access mode at first and then - and my message..i need to separate access mode and message.if i spitted like an array ther will be a problem if my message contains - character other than i appended –
0
NSArray *strArray = [myString componentsSeparatedByString:@"-"];

firstString = [strArray objectAtIndex:0];//Hai
secondString = [strArray objectAtIndex:1];//Welcome

Comments

0

This will be the solution if you are dealing with a string:

NSString *mySstring = @"hai-welcome";
NSMutableArray *anArray=[[NSMutableArray alloc] initWithArray:[componentsSeparatedByString: @"-"]];

And each word will be stored in respective position from 0-n.

Try This. :)

Comments

0

If you are averse to using arrays, you can consider this –

NSString *accessMode, *message;

NSScanner *scanner = [NSScanner scannerWithString:@"hai-welcome"];
NSCharacterSet *hyphenSet = [NSCharacterSet characterSetWithCharactersInString:@"-"];

[scanner scanUpToCharactersFromSet:hyphenSet intoString:&accessMode];
[scanner scanCharactersFromSet:hyphenSet intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@""] intoString:&message];

Comments

0
NSArray *lines = [string componentsSeparatedByString:@"-"];

The first value will be stored in 0th index of lines and second value will be stored in 1th index of lines..

Why not array?

Comments

0

Simple code :

NSString *myString = [[NSString alloc] initWithString:@"cat+dog+cow"];
NSArray *resultArray = [tempString componentsSeparatedByString:@"+"];

NSLog(@"1. %@ 2. %@ 3. %@",[resultArray objectAtIndex:0],[resultArray objectAtIndex:1],[resultArray objectAtIndex:2]);

Comments

0

Try This:

NSString *str = @"cat+dog+cow" ;
NSArray *array = [str componentsSeparatedByString:@"+"];
NSLog(@"%@",array) ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.