从txt文件截断字符串值并添加到数组

时间:2013-05-23 06:35:54

标签: iphone nsstring nsarray bundle

我有一个txt文件被复制到我的Xcode项目的支持文件.txt文件中的数据格式为:

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

我已使用以下代码成功从文件中提取数据:

NSString *greFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"grewords" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
self.greWordsArray = [NSMutableArray arrayWithArray:[greFileString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

当我打印greWordsArray时,我可以在日志中看到以下输出

"abacus@frame",
with,
balls,
for,
calculating,
"",
"abate@to",
lessen,
to,
subside,
"",
"abdication@giving",
up,
control,
authority,
"",
"aberration@straying",
away,
from,
what,
is,
normal,
"",

但是我想要两个独立阵列中的值,一个拿着算盘,减弱,退位,权威像差和其他阵列与框架与球计算,减少平息,放弃控制,偏离正常,即一个数组在@符号前保存字符串,在@符号后面包含

我知道有几种方法比如检查特殊字符方法,字符串通过替换字符串的出现,使用字符集,但事实是因为我的字符串greFileString是一个包含多个字符串的包,如果我只尝试这些方法中的任何一种算盘被添加到阵列中,但我希望将算盘,减弱,退位,像差添加到阵列中。

修改

根据H2CO3的建议,我实施了以下方式:

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

以下是记录的输出:

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

有人可以指导我吗?

2 个答案:

答案 0 :(得分:1)

这非常简单:如果字符串的第一个字符是'@',则将其放在一个数组中,否则将其放在另一个数组中。

< S>
NSArray *words = [greFileString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSMutableArray *at = [NSMutableArray array];
NSMutableArray *noAt = [NSMutableArray array];

for (NSString *s in words)
    if ([s characterAtIndex:0] == (unichar)'@')
        [at addObject:s];
    else
        [noAt addObject:s];

忽视上述情况 - OP对我说谎&gt;。文本文件实际上由行组成,其中符号分隔单词和解释,即。即

word1@explanation one
word2@explanation two

等。这意味着首先应检索这些行(可能使用- [NSString componentsSeparatedByString:]),然后将每行分成两部分(同样的方法在这里也很有用)。

答案 1 :(得分:0)

最后让它工作,最初我的想法是正确的,但无法正确执行。首先,我们需要从txt文件中获取数据并放入数组。然后,正如H2CO3所提到的,我们需要循环通过这里我们需要实现组件Separated By String。现在我们已经准备好了数据,需要做的是将数据放在数组中,使用数组对象,索引0为单词,1为提示,即:

NSString *greFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"grewords" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
self.greWordHints = [NSMutableArray array];
self.greWordsArray = [NSMutableArray array];
NSArray *greWords = [NSMutableArray arrayWithArray:[greFileString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
for (NSString *greWord in greWords)
{
   if (greWord && greWord.length)
   {
       NSArray *trimmedGreData = [greWord componentsSeparatedByString:@"@"];
       [greWordsArray addObject:[trimmedGreData objectAtIndex:0]];
       [greWordHints addObject:[trimmedGreData objectAtIndex:1]];
   }
}

希望它有所帮助,谢谢:)