Edited Answer:
OK - so you want the "last word" of each line.
But ---- we assume you want to ignore lines that don't have a "value" at the end. So:
- split the string into an array of "lines"
- create a list of lines to ignore
- loop through the array of lines
- IF the line is in the ignore list, ignore it
- else, get the last word and add it to our "last words" array
So...
NSString *searchedString = @"KANE458 SW19392 V1.13\n\nSERIAL No. 12345\n\nLOG No. 0002\n\nDATE 23/02/18 \nTIME 17:43:16\n\n------------------------\nNEXT CAL 11/12/18\n------------------------\n\nCOMMISSION TEST\n------------------------\n\nANALYSER ZERO\n-------------\nCO2 % -0.00\n\nCO ppm 0\n\nFLUE INTEGRITY\n--------------\n\nCO2 % 0.00\n\nMAX GAS FLOW\n------------\nCO2 % -0.00\nCO ppm 0\nCO/CO2 0.0000\n\nMIN GAS FLOW\n------------\nCO2 % -0.00\n\nCO ppm 0\n\nCO/CO2 0.0000\n\nFLOW & RETURN\n-------------\nT1 (null)C -N\F-\n\nT2 (null)C -N\F-\n\nDELTA (null)C -N\F-\n";
// lines to ignore, because they have no "values"
NSString *ignoreLines = @"COMMISSION TEST,ANALYSER ZERO,FLUE INTEGRITY,MAX GAS FLOW,MIN GAS FLOW,FLOW & RETURN";
// initialize an array for the last "word" from each line
NSMutableArray *arrayOfLastWords = [NSMutableArray array];
// split string into an array of "lines"
NSArray *arrayOfLines = [searchedString componentsSeparatedByString:@"\n"];
// for each "line"
for (NSString *s in arrayOfLines) {
// see if this line is listed as one of the lines to ignore
NSRange ignoreRange = [ignoreLines rangeOfString:s];
// if not found, then we want to get the last "word"
if (ignoreRange.location == NSNotFound) {
// find last space character
NSRange range = [s rangeOfString:@" " options:NSBackwardsSearch];
// if the line has a space
if (range.location != NSNotFound) {
// gett the last "word" - everything after the last space
NSString *result = [s substringFromIndex:range.location+1];
// append it to our array of last words
[arrayOfLastWords addObject:result];
}
}
}
NSLog(@"\n%@", arrayOfLastWords);
Gives a resulting array of:
(
"V1.13",
12345,
0002,
"",
"17:43:16",
"11/12/18",
"-0.00",
0,
"0.00",
"-0.00",
0,
"0.0000",
"-0.00",
0,
"0.0000",
"-NF-",
"-NF-",
"-NF-"
)
If you don't want the "V1.13" from the first line, just skip processing the first line.
If you don't know that the string will be returned with newLine separators, or if you don't know what strings will be returned that you need to ignore, then you need to write some additional code to split the string on various separators, and come up with criteria of what constitutes a "line with a value".
Original Answer:
You could do this with a "brute force" approach:
- Split the string into an array of "lines"
- loop through the lines
- check the first "word" in each line
- if it matches "CO2" or "T1", get the rest of the line (skipping spaces) and add it to an array
Or, you could use Regular Expressions. This is a partial approach:
NSString *searchedString = @"KANE458 SW19392 V1.13\n\nSERIAL No. 12345\n\nLOG No. 0002\n\nDATE 23/02/18 \nTIME 17:43:16\n\n------------------------\nNEXT CAL 11/12/18\n------------------------\n\nCOMMISSION TEST\n------------------------\n\nANALYSER ZERO\n-------------\nCO2 % -0.00\n\nCO ppm 0\n\nFLUE INTEGRITY\n--------------\n\nCO2 % 0.00\n\nMAX GAS FLOW\n------------\nCO2 % -0.00\nCO ppm 0\nCO/CO2 0.0000\n\nMIN GAS FLOW\n------------\nCO2 % -0.00\n\nCO ppm 0\n\nCO/CO2 0.0000\n\nFLOW & RETURN\n-------------\nT1 (null)C -N\F-\n\nT2 (null)C -N\F-\n\nDELTA (null)C -N\F-\n";
NSError *error = nil;
NSRange searchedRange = NSMakeRange(0, [searchedString length]);
// search for "CO2" at the start of a line, and capture everything to the end of the line
NSString *pattern = @"\\nCO2\\s*([^\\n\\r]*)";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
NSArray* matches = [regex matchesInString:searchedString options:0 range: searchedRange];
// for each found match, skip spaces and save the rest of the line
for (NSTextCheckingResult* match in matches) {
NSString* matchText = [searchedString substringWithRange:[match range]];
NSRange group1 = [match rangeAtIndex:1];
[arrayOfCO2 addObject:[searchedString substringWithRange:group1]];
}
// search for "T1" at the start of a line, and capture everything to the end of the line
pattern = @"\\nT1\\s*([^\\n\\r]*)";
regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
matches = [regex matchesInString:searchedString options:0 range: searchedRange];
// for each found match, skip spaces and save the rest of the line
for (NSTextCheckingResult* match in matches) {
NSString* matchText = [searchedString substringWithRange:[match range]];
NSRange group1 = [match rangeAtIndex:1];
[arrayOfT1 addObject:[searchedString substringWithRange:group1]];
}
NSLog(@"");
NSLog(@"CO2:");
NSLog(@"%@", arrayOfCO2);
NSLog(@"");
NSLog(@"T1:");
NSLog(@"%@", arrayOfT1);
This is the result from the sample string you posted:
CO2:
(
"% -0.00",
"% 0.00",
"% -0.00",
"% -0.00"
)
T1:
(
"(null)C -NF-"
)