2012-11-16 48 views
-1

我下面有這個NSString。我希望能夠得到每個應用程序的名稱和它的位置。有人能幫助我或指出我朝着正確的方向嗎? 在此先感謝。在NSString中獲取特定值

Applications: 

Xcode: 

    Version: 4.5.2 
    Last Modified: 11/3/12 11:45 PM 
    Kind: Intel 
    64-Bit (Intel): Yes 
    App Store: Yes 
    Location: /Applications/Xcode.app 

Terminal: 

    Version: 2.3 
    Last Modified: 6/21/12 12:01 AM 
    Kind: Intel 
    64-Bit (Intel): Yes 
    App Store: No 
    Location: /Applications/Utilities/Terminal.app 

Google Chrome: 

    Version: 23.0.1271.64 
    Last Modified: 10/31/12 7:59 PM 
    Kind: Intel 
    64-Bit (Intel): No 
    App Store: No 
    Location: /Applications/Google Chrome.app 

App Store: 

    Version: 1.2.1 
    Last Modified: 4/18/12 7:52 PM 
    Kind: Intel 
    64-Bit (Intel): Yes 
    App Store: No 
    Location: /Applications/App Store.app 

回答

0

如果有簡單的分隔符,我會使用componentsSeparatedByString。如果沒有,請嘗試使用帶NSRegularExpression的正則表達式來查找值

1

使用您的字符串創建NSScanner的實例。由於在默認情況下跳過空格和換行NSScanner默認的情況下,你需要繼續之前禁用此行爲,像這樣:

[scanner setCharactersToBeSkipped:[[[NSCharacterSet alloc] init] autorelease]]; 

從這裏,你會做這樣的事情:

NSString *appName = nil; 

[scanner scanString:@"Applications:" intoString:NULL]; 

NSCharacterSet *charset = [NSCharacterSet newlineCharacterSet]; 

[scanner scanCharactersFromSet:charset intoString:NULL]; 

while ([scanner isAtEnd] == NO) 
{ 
    // Get your application name. 
    [scanner scanUpToCharactersFromSet:charset intoString:&appName]; 
    [scanner scanCharactersFromSet:charset intoString:NULL]; 

    // You could do something with your application name here. 

    // Skip over the other stuff. 
    for (NSUInteger idx = 0; idx < 6; idx++) 
    { 
     [scanner scanUpToString:@":" intoString:NULL]; 
     [scanner scanString:@":" intoString:NULL]; 
     [scanner scanUpToCharactersFromSet:charset intoString:NULL]; 
     [scanner scanCharactersFromSet:charset intoString:NULL]; 
    } 
} 

請注意,儘管出現,我沒有給你一個完整的解決方案。除了檢查字符串的結尾之外,沒有錯誤檢查,並且不用說RealApplication檢查錯誤。 (甚至應該在開始時檢查字符串結尾。)此外,這段代碼依賴於數據的嚴格表示,例如您提供的。

0

有很多方法可以解決這個問題。 NSRegularExpression已被提及。另一個是NSScanner。這裏有一個潛在的解決方案(沒有關於正確性的保證 - 只是一個指南。)

#import <Foundation/Foundation.h> 

int main(int argc, const char * argv[]) 
{ 

    @autoreleasepool { 
     NSString *source = @"Applications:\n\nXcode:\n\n Version: 4.5.\n Last Modified: 11/3/12 11:45 PM\n Kind: Intel\n 64-Bit (Intel): Yes\n App Store: Yes\n Location: /Applications/Xcode.app"; 
     NSScanner *scanner = [NSScanner scannerWithString:source]; 
     [scanner scanUpToString:@":" intoString:NULL]; 
     [scanner scanString:@":" intoString:NULL]; 

     while(![scanner isAtEnd]) { 
      NSString *appName = nil; 
      NSString *appPath = nil; 

      [scanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:NULL]; 
      [scanner scanCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&appName]; 

      NSString *junk = nil; 
      [scanner scanUpToString:@"Location: " intoString:&junk]; 
      [scanner scanString:@"Location: " intoString:NULL]; 
      [scanner scanUpToString:@"\n" intoString:&appPath]; 
      [scanner scanString:@"\n" intoString:NULL]; 

      printf("app name = %s, path = %s\n",[appName UTF8String],[appPath UTF8String]); 
     } 

    } 
    return 0; 
}