I have a txt file which was copied to Supporting Files of my Xcode project.The data in txt file is of format:
abacus@frame with balls for calculating
abate@to lessen to subside
abdication@giving up control authority
aberration@straying away from what is normal
....................around 4000 lines
I have successfully extracted data from the file using the below code:
NSString *greFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"grewords" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
self.greWordsArray = [NSMutableArray arrayWithArray:[greFileString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
When I print greWordsArray,I could see the below output in log
"abacus@frame",
with,
balls,
for,
calculating,
"",
"abate@to",
lessen,
to,
subside,
"",
"abdication@giving",
up,
control,
authority,
"",
"aberration@straying",
away,
from,
what,
is,
normal,
"",
But I want the values in two separate arrays,one holding abacus,abate,abdication,authority aberration and other array with frame with balls for calculating,to lessen to subside,giving up control,straying away from what is normal i.e. one array holding string before @ symbol and one with after @ symbol
I know there are several methods like checking for special character method,string by replacing occurrences of string,using character set,but the fact is since my string greFileString is a bundle holding multiple strings,if I try any of these methods only abacus is getting added to array,but I want abacus,abate,abdication,aberration to be added to array.
EDIT
Following suggestion of H2CO3,I have implemented the following way:
NSString *greFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"grewords" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
NSArray *greData = [NSArray arrayWithArray:[greFileString componentsSeparatedByString:@"@"]];
self.greWordsArray = [NSMutableArray array];
self.greWordHints = [NSMutableArray array];
for (NSString *greWord in greData)
{
if ([greWord characterAtIndex:0] == (unichar)'@')
{
[greWordHints addObject:greWord];
}
else
{
[greWordsArray addObject:greWord];
}
}
NSLog(@"gre words are %@",greWordsArray);
NSLog(@"gre hints are %@",greWordHints);
Here is the logged output:
gre words are (
abacus,
"frame with balls for calculating
\nabate",
"to lessen to subside
\nabdication",
"giving up control authority
\naberration",
"straying away from what is normal
\nabet",
"help/encourage somebody (in doing wrong)
\nabeyance",
"suspended action
\nabhor",
"to hate to detest
gre hints are (
)
Can someone please guide me on this?