Split NSArray based on characters

Arrays are omnipresent. As I needed a snippet to make a string into an array of values, I thought would be helpful to share this for beginners.

For example, take a string variable like,

NSString *str = [[NSString alloc] initWithFormat:@"This##is##string##conversion##example"];

Create an array variable „array1” as:-

NSArray *array1 = [[NSArray alloc] initWithArray:[str componentsSeparatedByString:@"##"]];
for (int i=0;i<array1.count; i++) {
   NSLog(@"place name at index %d: %@\n", i, [array1 objectAtIndex:i]);
}

I used the for loop to know the array1 values, which is displayed in Console output:

2013-10-17 01:26:12.335 PickerViewTest[21740:a0b] place name at index 0: This
2013-10-17 01:26:12.337 PickerViewTest[21740:a0b] place name at index 1: is
2013-10-17 01:26:12.337 PickerViewTest[21740:a0b] place name at index 2: string
2013-10-17 01:26:12.338 PickerViewTest[21740:a0b] place name at index 3: conversion
2013-10-17 01:26:12.338 PickerViewTest[21740:a0b] place name at index 4: example

Hope it helps.

 
 
Filed in Code Snippets & Hints, How To's and tagged , , , . Bookmark the permalink.

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *