我想从字符串下方选择所有测量值,并需要存储到一个数组中。我从名字是“Kane”的机器上得到这种类型的字符串。当我当时使用蓝牙连接到这台机器时,我得到了这种类型的字符串。我可以将此字符串打印到控制台中。但我无法从这个字符串中检索值,我想存储到一个数组中。任何人都可以帮助我。谢谢
我想将 [serial no,Log No,DATE,TIME,CO2,CO,CO2,CO2,CO,CO/CO2,T1,T2,DELTA] 的值存储在一个数组中,例如:[12345,0002,23 /02/18,17:43:16, -0.00,0,0.00,-0.00,0,0.000,-N\F-,-N\F-,-N\F-]。这是我实际从机器获取并打印到 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-
我想要一个包含每一行最后一个空格字符之后的所有内容的数组
编辑答案:
好的 - 所以你想要每一行的“最后一句话”。
但是 ---- 我们假设您想忽略最后没有“值”的行。所以:
所以...
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);
给出一个结果数组:
(
"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-"
)
如果您不想要第一行中的“V1.13”,只需跳过处理第一行。
如果您不知道字符串将使用 newLine 分隔符返回,或者如果您不知道将返回哪些字符串需要忽略,那么您需要编写一些额外的代码来将字符串拆分为各种分隔符,并提出构成“价值线”的标准。
原答案:
你可以用“蛮力”方法来做到这一点:
或者,您可以使用正则表达式。这是部分方法:
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);
这是您发布的示例字符串的结果:
CO2:
(
"% -0.00",
"% 0.00",
"% -0.00",
"% -0.00"
)
T1:
(
"(null)C -NF-"
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句