如何从 NSString 获取特定的字符串?
How to get specific strings from NSString?
我有一个字符串,例如:
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:E250、E103、E1255上面的文本并与我预定义的 NSArray(E100, E101,...E1500) 元素进行比较
有什么办法吗?
与其从文本中提取字符串,我建议您从数组中查找模板,例如:
NSString *input = @"Sodium Nitrite E250 is a food additive ...";
for (NSString *str in @[ @"E100", @"E101", @"E1500" ])
if ([input rangeOfString:str].location != NSNotFound) {
// do something
}
尝试使用此代码
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
希望对您有所帮助。
看看这个:
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
请尝试以下逻辑,这可能会解决您的问题。
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);
如您所说,您想与现有的数组元素进行比较。所以你可以做下面的..
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);
}
}];
您可以通过执行以下操作从 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
)
希望对您有所帮助。
我有一个字符串,例如:
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:E250、E103、E1255上面的文本并与我预定义的 NSArray(E100, E101,...E1500) 元素进行比较 有什么办法吗?
与其从文本中提取字符串,我建议您从数组中查找模板,例如:
NSString *input = @"Sodium Nitrite E250 is a food additive ...";
for (NSString *str in @[ @"E100", @"E101", @"E1500" ])
if ([input rangeOfString:str].location != NSNotFound) {
// do something
}
尝试使用此代码
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
希望对您有所帮助。
看看这个:
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
请尝试以下逻辑,这可能会解决您的问题。
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);
如您所说,您想与现有的数组元素进行比较。所以你可以做下面的..
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);
}
}];
您可以通过执行以下操作从 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
)
希望对您有所帮助。