2012-12-14 41 views
-1

我是xcode的新手,也許這個問題很愚蠢,但我的工作滯後於我的家庭。
在文件中,我想讀取字符串後面的特定字符。之後的字符串相同

例如:

asasasasasas 
wewewewewewe 
xyz_ 22 aaaaaaaaaaa bbbbbbbbbbb ccccccccccccccc ddddddddddddd 
fgfgfgfgfgfgfg 
ererererererer 

abc_ 12 bbbbbbbbbb dddddddd 
jkjkjkjkjkjkjk 
lalallalalalal 

mycode的:

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 
+0

請再次用2個例子來解釋你的問題和要求。我格式化了你的問題,我錯過了什麼? –

+0

我想「xyz_」後打印的字符我想打印所有包含在每個字符之間的空間像xyz_後charachter: AAAAAAAA BBBBBBBB CCCCCCC 不說「的NSArray * lineFile = [contentFile componentsSeparatedByString :@」「] ..我嘗試它不工作 – DD007

+0

你要打印 「在你的例子22 AAAAAAAAAAA bbbbbbbbbbb ccccccccccccccc ddddddddddddd」 –

回答

1

一切都是w獸人很好.. ,但我想xyz_後打印...像:

AAAAAAAAAAA
bbbbbbbbbbb
ccccccccccccccc
ddddddddddddd

我希望能有正確的理解,你想做什麼:

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"]); 
} 
+0

謝謝:)我想這它的工作原理,但我希望把每行一個標記爲前: AAAAAAAAA bbbbbbbbb ccccccccc DDDDDDDDD DD007

+0

這只是一個例子嗎?您應該準確解釋應如何添加標籤的規則。 –

+0

雅傢伙得到了輸出感謝幫助:)感謝很多 – DD007

0

字符串「測試」不存在於要格式化的字符串中。嘗試刪除它,然後嘗試與[內容文件componentsSeparatedByString @」「]

+0

謝謝:)我想這它的工作原理,但我希望把每行一個標記爲前: AAAAAAAAA bbbbbbbbb ccccccccc DDDDDDDDD DD007

+0

取從componentsSeparatedByString獲取的每個元素的前三個字符和最後三個添加所需的任何標記。 – andy

0

如果你想打破由新線串,試試這個:

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

,而不是

NSArray *lineFile = [contentFile componentsSeparatedByString:@"\n"]; 
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*)" 

這種模式有三個字符後面緊跟着兩個數字,然後用空間(S)下劃線和空間(S)的任何行相匹配。

您可以使用這些功能,然後根據自己的需要:

firstMatchInString:options:range:

matchesInString:options:range:

numberOfMatchesInString:options:range:

希望它能幫助。

快樂編碼和閱讀!

編輯:

我已經更新代碼,但我有疑問的沒有。您在評論中指定的標記中的字符。那麼這會給你一個關於如何解決這個問題的想法。

+0

謝謝:)我想這是工作,但我希望把每行前標籤: AAAAAAAAA bbbbbbbbb ccccccccc DDDDDDDDD DD007

+0

@ DD007,看看我的更新,我已經更新了代碼和添加新的for循環,根據需要修改數值。 – user1873574

+0

完美酷:)感謝您的幫助:) – DD007

相關問題