如何从NSString获取特定字符串?

时间:2015-05-05 13:02:17

标签: ios objective-c search nsstring nsarray

我有一个字符串,如:

  

亚硝酸钠 E250 是一种食品添加剂,可以提供腌制肉类,   如火腿 E103 ,培根,热狗,法兰克福香肠,烟熏鱼    E1255 咸牛肉,其特有的红色和风味。

我需要从上面的文字中获得NSArray元素: E250 E103 E1255 ,并与我预定义的NSArray(E100, E101,... E1500)元素 有什么方法可以做到这一点吗?

6 个答案:

答案 0 :(得分:0)

我建议您从数组中找到模板,而不是从文本中提取字符串,而不是:

> library("RCurl")
Loading required package: bitops
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RCurl_1.95-4.6 bitops_1.0-6  

loaded via a namespace (and not attached):
[1] tools_3.2.0
> system.file()
[1] "/Library/Frameworks/R.framework/Resources/library/base"
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"

答案 1 :(得分:0)

尝试使用此代码

NSArray *ary = [NSArray arrayWithObjects:@"E250", nil]; // String Data to find in String
NSMutableArray *aryFoundData = [[NSMutableArray alloc]init];

for (int i=0; i<ary.count; i++) {
    NSString *str = @"Your String E250";
    if ([str containsString:[ary objectAtIndex:i]]) {
        [aryFoundData addObject:[ary objectAtIndex:i]];
    }
}

// Now aryFoundData contains all the data which are available in string

希望这会对你有所帮助。

答案 2 :(得分:0)

看看这个:

NSString *testString = @"Sodium Nitrite E250 is a food additive that gives cured meats, such as ham E103, bacon, hot dogs, frankfurters, smoked fish E1255 corned beef, their characteristic red colour and flavour.";

NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"E\\d+" options:0 error:nil];

[regex enumerateMatchesInString:testString options:0 range:NSMakeRange(0, testString.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSLog(@"%@", [testString substringWithRange:result.range]);
}];

记录以下内容:

2015-05-05 15:12:55.898标签[3649:339896] E250

2015-05-05 15:12:55.898标签[3649:339896] E103

2015-05-05 15:12:55.898标签[3649:339896] E1255

答案 3 :(得分:0)

请尝试下面的逻辑可能是你的问题解决。

NSMutableArray *ar = [NSMutableArray new];
NSString *str = @"Sodium Nitrite E250 is a food additive that gives cured meats, such as ham E103, bacon, hot dogs, frankfurters, smoked fish E1255 corned beef, their characteristic red colour and flavour.";

NSArray * words = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
for (NSString * word in words) {
    if ([word length] > 0) {
        NSString * firstLetter = [word substringToIndex:1];
        if ([firstLetter isEqualToString:@"E"]) {
            [ar addObject:word];
        }
    }
}
NSLog(@"%@",ar);

答案 4 :(得分:0)

正如您所说,您想要与现有的数组元素进行比较。所以你可以在下面做..

NSArray *array = @[@"E100", @"E250", @"E103", @"E200", .....];
NSString *myString = @"Sodium Nitrite E250 is a food additive that gives cured meats, such as ham E103, bacon, hot dogs, frankfurters, smoked fish E1255 corned beef, their characteristic red colour and flavour.";

[array enumerateObjectsUsingBlock:^(NSString *objectString, NSUInteger idx, BOOL *stop) {
    if([myString rangeOfString:objectString].location != NSNotFound) {
        NSLog(@"%@ - found", objectString);
    }
    else {
        NSLog(@"%@ - Not found", objectString);
    }
}];

答案 5 :(得分:0)

您可以从NSString获取特定关键字,并通过执行以下操作将其添加到NSMutableArray

 NSString* strText = @"Sodium Nitrite E250 is a food additive that gives cured meats, such as ham E103, bacon, hot dogs, frankfurters, smoked fish E1255 corned beef, their characteristic red colour and flavour.";
    __block NSMutableArray* arrayStoreRange = [NSMutableArray new];

    //To Find Image from HTML string and add renge in NSMutableArray
    //width
    NSRegularExpression* tableRegex = [[NSRegularExpression alloc] initWithPattern:@"E\\w*\\d" options:0 error:NULL] ;
    [tableRegex enumerateMatchesInString:strText options:0 range:NSMakeRange(0, [strText length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){

        [arrayStoreRange addObject:[strText substringWithRange:match.range]];

    }];

    NSLog(@"Output : %@",arrayStoreRange);

    for (NSString *str in arrayStoreRange){

        if ([yourArray containsObject:str]) {
            // do something
            NSLog(@"Object found");
        }
        else{
            NSLog(@"Object  not found");
        }
    }

输出:

Output : (
    E250,
    E103,
    E1255
)

希望这对你有所帮助。