如何在ios objective-c中从未格式化的字符串中获取值?

时间:2018-03-19 11:41:21

标签: ios objective-c

我想从下面的字符串中选择所有测量值,并且需要存储到一个数组中。我从机器上得到这种类型的字符串,名字是“Kane”。当我用蓝牙连接这台机器的时候,我得到这种类型的字符串。我能够将此字符串打印到控制台中。但我无法从该字符串中检索值,我想存储到数组中。任何人都可以帮助我。感谢

我要存储在一个单独的阵列[序列号,登录号,日期,时间,CO 2,CO,CO 2,CO 2,CO,CO / CO 2,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-

我想要一个数组,其中包含每一行中最后一个空格字符后的所有内容

2 个答案:

答案 0 :(得分:0)

似乎字符串有新行(假设这是一个静态文本)。这将检索值:

斯威夫特:

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]  

目标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];

答案 1 :(得分:0)

编辑答案:

好的 - 所以你想要"最后一个字"每一行。

但是----我们假设您想要忽略那些没有"值"在末尾。所以:

  • 将字符串拆分为"行"
  • 的数组
  • 创建要忽略的行列表
  • 遍历行数组
  • 如果该行在忽略列表中,则忽略它
  • 否则,得到最后一个字并将其添加到我们的"最后一个字"阵列

因此...

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分隔符返回,或者您不知道将返回哪些字符串需要忽略,那么您需要编写一些额外的代码将字符串拆分为各种分隔符,并提出构成带有值#34的"行的标准。

原始答案:

你可以通过"蛮力"的方法:

  • 将字符串拆分为"行"
  • 的数组
  • 循环线
  • 检查第一个"字"在每一行
  • 如果匹配" CO2"或" T1",获取行的其余部分(跳过空格)并将其添加到数组

或者,您可以使用正则表达式。这是部分方法:

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-"
)