-1

I Have a NSString which contains following string:

 "Model Name: Mac mini
  Model Identifier: Macmini6,1
  Processor Name: Intel Core i5
  Processor Speed: 2.5 GHz
  Number of Processors: 1
  Total Number of Cores: 2
  L2 Cache (per Core): 256 KB
  L3 Cache: 3 MB
  Memory: 4 GB
  Boot ROM Version: MM61.0106.B03
  SMC Version (system): 2.7f1
  Serial Number (system): C07M81SWDWYL
  Hardware UUID: 3B2564A0-7F96-5774-9C93-E56769E9344D"

I want to retrive Information about Processor name and Model name into another nsstring. How to do it.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

2 Answers2

1

Unoptimal, quick-and-dirty and string-format-dependent solution:

-(NSString*) getValueFromString:(NSString *)text forTag:(NSString*)tag withNextTag:(NSString*)nextTag
{
    NSRange tagRange = [text rangeOfString:[tag stringByAppendingString:@": "]];
    NSRange nextTagRange = [text rangeOfString:[nextTag stringByAppendingString:@":"]];
    NSUInteger start = tagRange.location + tagRange.length;
    NSUInteger length = nextTagRange.location - start - 1; //-1 to skip a space before the next tag name
    return [text substringWithRange:NSMakeRange(start, length)];
}

Usage:

NSString *input = @"Model Name: Mac mini Model Identifier: Macmini6,1 Processor Name: Intel Core i5 Processor Speed: 2.5 GHz Number of Processors: 1 Total Number of Cores: 2 L2 Cache (per Core): 256 KB L3 Cache: 3 MB Memory: 4 GB Boot ROM Version: MM61.0106.B03 SMC Version (system): 2.7f1 Serial Number (system): C07M81SWDWYL Hardware UUID: 3B2564A0-7F96-5774-9C93-E56769E9344D";

NSLog(@"test: %@", [self getValueFromString:input forTag:@"Processor Name" withNextTag:@"Speed"]);

Also, you could try to find information about regular expressions.

jscs
  • 63,694
  • 13
  • 151
  • 195
FreeNickname
  • 7,398
  • 2
  • 30
  • 60
  • 2
    Might work, assuming the formatting doesn't change. I wouldn't rely on it though. In my opinion, the "correct" answer to this question is to get the information in a way that doesn't require string parsing. – CrimsonChris May 21 '14 at 06:25
1

This is again a string-format-dependent solution and assumption that each line is separated by "\n".

 NSString *string = @"Model Name: Mac mini\nModel Identifier: Macmini6,1\nProcessor Name: Intel Core i5\nProcessor Speed: 2.5 GHz\nNumber of Processors:1\nTotal Number of Cores:2\nL2 Cache (per Core): 256 KB\nL3 Cache: 3 MB\nMemory: 4 GB\nBoot ROM Version: MM61.0106.B03\nSMC Version (system): 2.7f1\nSerial Number (system): C07M81SWDWYL\nHardware UUID: 3B2564A0-7F96-5774-9C93-E56769E9344D";


NSArray *array = [string componentsSeparatedByString:@"\n"];

NSMutableDictionary *dict = [NSMutableDictionary new];

for (NSString *string in array) {
    NSString *key = [string componentsSeparatedByString:@":"][0];
    NSString *value = [string componentsSeparatedByString:@":"][1];

    [dict setObject:value forKey:key];
}

NSLog(@"Model Name : %@", dict[@"Model Name"]);
NSLog(@"Processor Name : %@", dict[@"Processor Name"]);
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140