Reputation: 35
i like to know how to fetch the specific string which starts with &abc and ends with &. I tried with had prefix and sufix . but this is not new line ,
&xyz;123:183:184:142&
&abc;134:534:435:432&
&qwe;323:535:234:532&
my code :
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanUpToString:@"&abc" intoString:nil]; //
NSString *substring = nil;
[scanner scanString:@"&abc" intoString:nil]; // Scan the # character
if([scanner scanUpToString:@"&" intoString:&substring]) {
// If the space immediately followed the &, this will be skipped
[substrings addObject:substring];
NSLog(@"substring is :%@",substring);
}
// do something with substrings
[substrings release];
how to make "scanner scanUpToString:@"&abc" and count ":"==3 till "#"???? can help me
Upvotes: 0
Views: 1184
Reputation: 13600
NSArray *arr = [NSArray arrayWithObjects:@"&xyz;123:183:184:142&",
@"&abc;134:534:435:432&",
@"&qwe;323:535:234:532&",
@"& I am not in it",
@"&abc I am out &" ,nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@ AND self ENDSWITH[cd] %@",@"&abc",@"&"];
NSLog(@"Sorted Array %@",[arr filteredArrayUsingPredicate:predicate]);
NSArray *sortedArray = [arr filteredArrayUsingPredicate:predicate];
NSMutableArray *finalResult = [NSMutableArray arrayWithCapacity:0];
for(NSString *string in sortedArray)
{
NSString *content = string;
NSRange range1 = [content rangeOfString:@"&abc"];
if(range1.length > 0)
content = [content stringByReplacingCharactersInRange:range1 withString:@""];
NSRange range2 = [content rangeOfString:@"&"];
if(range2.length > 0)
content = [content stringByReplacingCharactersInRange:range2 withString:@""];
[finalResult addObject:content];
}
NSLog(@"%@",finalResult);
Upvotes: 3
Reputation: 5200
Try using NSRegularExpression:
- (BOOL)isValidString:(NSString *)string
{
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"^&abc.*&$" options:0 error:NULL];
NSTextCheckingResult *result = [regularExpression firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
return (result != nil);
}
Upvotes: 0