-1

我是 xcode 的新手,也许这个问题很愚蠢,但我的家庭作业落后了。
在一个文件中,我想在字符串之后读取一个特定的字符。

例子:

asasasasasas
wewewewewewe
xyz_ 22 aaaaaaaaaaa bbbbbbbbbbb ccccccccccccccc ddddddddddddd
fgfgfgfgfgfgfg
ererererererer

abc_ 12 bbbbbbbbbb dddddddd
jkjkjkjkjkjkjk
lalallalalalal

我的代码:

NSString *contentFile = [[NSString alloc] initWithContentsOfFile:pathFile    encoding:NSUTF8StringEncoding error:nil];
NSArray *lineFile = [contentFile componentsSeparatedByString:@"\n"];
NSMutableString *xmlFile = [[NSMutableString alloc] init];
for(int i = 2; i < lineFile.count; i++)//i = 2 for don't take the 2 first line
{
    if ([((NSString *)[lineFile objectAtIndex:i]) rangedOfString:@"test"].location !=  NSNotFound){
        xmlFile = [NSString stringWithFormat:@"%@<nameTag>%@</nameTag>", xmlFile, (NSString *)[lineFile objectAtIndex:i]];
    }
    else if ...
}

一切正常..
但我想在 xyz_ 之后打印...比如:

aaaaaaaaaaa 
bbbbbbbbbbb 
ccccccccccccccc 
ddddddddddddd
4

4 回答 4

1

你也可以试试这个:(建议在单独的测试应用程序中尝试)

我假设你的目标线:

xyz_ 22 aaaaaaaaaaa bbbbbbbbbbb ccccccccccccccc ddddddddddddd

总是以“xyz_ 22”开头。在这里查看:(另请参阅评论)

    // prepared a string similar to what you already have
    NSMutableString *xmlfile = [[NSMutableString alloc] initWithFormat:@"%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@",
    @"asasasasasas",
    @"wewewewewewe",
    @"xyz_ 22 aaaaaaaaaaa bbbbbbbbbbb ccccccccccccccc ddddddddddddd",
    @"fgfgfgfgfgfgfg",
    @"ererererererer",
    @"",
    @"abc_ 12 bbbbbbbbbb dddddddd",
    @"jkjkjkjkjkjkjk",
    @"lalallalalalal"];

    NSLog(@"%@", xmlfile);    // check out by printing (optional)

    NSArray *arr = [xmlfile componentsSeparatedByString:@"\n"];  // first break with NEWLINE character
    NSLog(@"%@",arr);         // check out by printing (optional)


 for (NSString *str in arr)    // traverse all lines
{
    if([str hasPrefix:@"xyz_ 22"])  // if it starts with "xyz_ 22"
    {

        NSMutableArray *mArr = [[NSMutableArray alloc] initWithArray:[[str stringByReplacingOccurrencesOfString:@"xyz_ 22 " withString:@""] componentsSeparatedByString:@" "]];

         for(int i=0; i< [mArr count];i++ )
         {
            NSString *tag;

            if([[mArr objectAtIndex:i] length] > 3)
            {
                // If more than three i.e., aaaaa then write <aaa>aaaaaaa<aaa>
                tag = [[mArr objectAtIndex:i] substringWithRange:NSMakeRange(0, 3)];
            }
            else
            {
                // If less than three i.e., aa then pick <aa>aa<aa> or 
                                            a  then pick <a>a<a>
                tag = [mArr objectAtIndex:i];
            }

            NSString *s= [[NSString alloc] initWithFormat:@"<%@>%@<%@>", tag, [mArr objectAtIndex:i], tag];
            [mArr removeObjectAtIndex:i];
            [mArr insertObject:s atIndex:i];

        }

        NSLog(@"%@", mArr);   // prints output
    }
}

如果行首不固定为“xyz_ 22”,则需要查看NSRegularExpression类并使用它而不是使用hasPrefix.

此示例模式可以帮助您:

@"^(.{3}\_\s*\d{2}\s*)"

此模式匹配包含三个字符后跟下划线和空格后跟两位数字后跟空格的任何行。

您可以根据需要使用这些功能中的任何一个:

firstMatchInString:选项:范围:

匹配字符串:选项:范围:

numberOfMatchesInString:选项:范围:

希望能帮助到你。

快乐的编码和阅读!!!

编辑:

我已经更新了代码,但我怀疑没有。您在评论中指定的标签中的字符数。好吧,这将使您了解如何解决此问题。

于 2012-12-14T16:13:06.383 回答
1

一切正常..但我想在 xyz_ 之后打印...比如:

aaaaaaaaaa
bbbbbbbbbbb
ccccccccccccccc
dddddddddddddd

我希望能正确理解你想要做什么:

if ([([lineFile objectAtIndex:i]) rangedOfString:@"test"].location !=  NSNotFound)
{
    xmlFile = [NSString stringWithFormat:@"%@<nameTag>%@</nameTag>", xmlFile, (NSString *)[lineFile objectAtIndex:i]];
    NSString* str= [[lineFile objectAtIndex: i]stringByReplacingOccurrencesOfString: @"xyz_ " withString: @""];
    NSLog(@"%@",[str stringByReplacingOccurrencesOfString: @" " withString: @"\n"]);
}
于 2012-12-14T13:05:04.480 回答
0

If you want to break the string by new line, try this one :

NSArray *lineFile=[contentFile componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

instead of

NSArray *lineFile = [contentFile componentsSeparatedByString:@"\n"];
于 2012-12-14T13:24:39.307 回答
0

要格式化的字符串中不存在字符串“test”。尝试将其删除,然后尝试使用 [contentFile componentsSeparatedByString@" "]

于 2012-12-14T13:05:52.827 回答