0

I want to pick all the measurement values from below string and need to store into one array. I am getting this type of string from machine whoes name is "Kane". when I connected with this machine using bluetooth at that time I am getting this type of string. I am able to print this string into console. but I am not able to retrive values from this string and I want to store into an array. Can anyone please help me out. Thanks

i want to store values of [serial no,Log No,DATE,TIME,CO2,CO,CO2,CO2,CO,CO/CO2,T1,T2,DELTA] in one single array, like: [12345,0002,23/02/18,17:43:16, -0.00,0,0.00,-0.00,0,0.000,-N\F-,-N\F-,-N\F-]. here is the string which i actually get from machine and print into textview:

KANE458 SW19392 V1.13

SERIAL No.    12345

LOG No.               0002

DATE          23/02/18  
TIME          17:43:16

------------------------
NEXT CAL      11/12/18
------------------------

COMMISSION TEST
------------------------

ANALYSER ZERO
-------------
CO2             %  -0.00

CO            ppm      0

FLUE INTEGRITY
--------------

CO2             %   0.00

MAX GAS FLOW
------------
CO2             %  -0.00
CO            ppm      0
CO/CO2            0.0000

MIN GAS FLOW
------------
CO2             %  -0.00

CO            ppm      0

CO/CO2            0.0000

FLOW & RETURN
-------------
T1               (null)C  -N\F-

T2             (null)C  -N\F-

DELTA          (null)C  -N\F-

I want an array containing everything after the last space character from every line

Abhinaba
  • 17
  • 1
  • 2
  • 7
  • is there and specific prefix or suffix?, so you can extract that value from it using regex pattern. – Ravi Panchal Mar 19 '18 at 11:48
  • hi ravi yes, CO2.CO,O2,T1 are always same but all time values are different. separate by newline(\n) is not working. can you give an example of regax pattern? Thanks – Abhinaba Mar 19 '18 at 11:57
  • May be this answer will help you look in to this https://stackoverflow.com/questions/27880650/swift-extract-regex-matches and for creating your own regex you can use this site: https://regexr.com/ . still you require any help so let me know. – Ravi Panchal Mar 19 '18 at 12:00
  • i think regax will help me. if you know how to ceate regax please help me. – Abhinaba Mar 19 '18 at 12:12
  • store the all string in your array , and iterate your array using components(separatedBy: " " ) , and store the first object to your new array\ – Anbu.Karthik Mar 19 '18 at 12:15
  • i have used same trick but problem is that string is dynamic so when i separate with newline and space, some time we are getting different index. i hope you understand. i need to read line by line but how i dont know. – Abhinaba Mar 19 '18 at 12:17
  • Hi ravi, i have updated my question. have a look – Abhinaba Mar 19 '18 at 12:33
  • You say you want *"CO2,O2,T1's values"* ... I see a `T1` line and multiple `CO2` lines, but no `O2` line... What parts do you actually want to get? Show an example of the array that you want as a result of parsing this string... – DonMag Mar 19 '18 at 13:48
  • Hi DonMag,actually i want all the measurement values whatever machine sent. any question? – Abhinaba Mar 19 '18 at 13:55
  • So, do you want this array as a result? `["-0.00", "0.00", "-0.00", "-0.00", "(null)C -N\F-"]`? Or do you want one array of the CO2 values, one array of O2 values and one array of T1 values? And should "CO/CO2" be included? – DonMag Mar 19 '18 at 14:01
  • Yes DonMag, just want to store values, like: ["-0.00", "0.00", "-0.00", "-0.00", " -N\F-"]. dont need units, like: %,(null)C. just need to store all measurement values into single array. If possible please help me. Thanks – Abhinaba Mar 19 '18 at 14:22

2 Answers2

0

It seems that the string has new lines (Assuming this is a static text). This would retrieve the values:

Swift :

let stringVal = "CO2 % -23.0\n\nO2 % -0.00\n\nT1 ppm -N/_F"
let arrayNewLine = stringVal.components(separatedBy: "\n")
let strCO2 = arrayNewLine[0].components(separatedBy: " ")[0]
let strO2 = arrayNewLine[1].components(separatedBy: " ")[0]
let strT1 = arrayNewLine[2].components(separatedBy: " ")[0]  

Objective C :

NSString *stringVal = @"CO2 % -23.0\n\nO2 % -0.00\n\nT1 ppm -N/_F";
NSArray *arrNewLine = [stringVal componentsSeparatedByString:@"\n"];
NSString *strCO2 = [[[arrNewLine objectAtIndex:0] componentsSeparatedByString:@" "] objectAtIndex:0];
NSString *strO2 = [[[arrNewLine objectAtIndex:1] componentsSeparatedByString:@" "] objectAtIndex:0];
NSString *strT1 = [[[arrNewLine objectAtIndex:2] componentsSeparatedByString:@" "] objectAtIndex:0];
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • hi Nitish, separate by newline is not working. is it possible to read line by line form this string? – Abhinaba Mar 19 '18 at 11:58
  • May I know that the text is static or dynamic ? I have updated the answer. It seems there are 2 new lines in the text you have shared in the question. Try it out. – Nitish Mar 19 '18 at 12:01
  • text is dynamic. when I use this trick some time it works but some time indexes are messup. any idea how to handle all the time, whatever machine send. – Abhinaba Mar 19 '18 at 12:06
  • If it is giving issue with indices, that means that the dynamic string is not of the same format always. – Nitish Mar 19 '18 at 12:07
  • I would require to know how does the format vary of the dynamic text and what EXACTLY you want ? If possible share 4-5 dynamic values. – Nitish Mar 19 '18 at 12:08
  • please see the questions,i have added an image. like an image same string i got while connect with machine and then print into textview. – Abhinaba Mar 19 '18 at 12:15
  • Please share the **text** which you are getting, not the image. – Nitish Mar 19 '18 at 12:18
  • Hi Nitish, i have updated my question, please help me – Abhinaba Mar 19 '18 at 12:27
0

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-"
)
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Thanks for your effort. But i think we have some miss unstanding. i want all the values. measurement name are : [Serial no, Log no, Sate, Time, CO2,CO,CO2,CO2,CO,CO/CO2,CO2,CO,CO/CO2,T1,T2,Delta]. so i need this measurement values into single array, like: [12345,0002,-0.00,0,0.00,-0.00,0,0.0000,-0.00,0,0.0000,-N\F-,-N\F-,-N\F-]. is that possible? – Abhinaba Mar 19 '18 at 14:33
  • OK - you need to think about how you ask your question. So, from the sample string, you want an array containing everything after the last space character from every line? – DonMag Mar 19 '18 at 14:36
  • Yes DonMag, you are right.i want an array containing everything after the last space character from every line. Please modify your ans as i want. it's verry helpfull for me. – Abhinaba Mar 19 '18 at 14:41
  • @ DonMag, if i want ignore any of line then just put into strings object ignoreLines? – Abhinaba Mar 19 '18 at 15:41
  • @Abhinaba - Do you really need to ask? Try it, and see what happens. – DonMag Mar 19 '18 at 16:12
  • Thanks DonMag, this code is working correctly what I want. Again thanks for your help. – Abhinaba Mar 19 '18 at 16:29
  • @Abhinaba - for the benefit of others who see this, be sure to mark the answer as Accepted if it does what you need. – DonMag Mar 19 '18 at 17:27