问题
I have a lengthy string as below
Please plan to attend to provide upgrade to existing code
morning meeting to acoomdate bum team members
Meeting Number: 457 231 123
To join this meeting
- go to http://domainname.com
- enter password
Now i want to grab number after the text "Meeting Number" i.e. 457 231 123
Any help please. thanks
EDIT
Lets say i have a string
NSString *myString = @"Please plan to attend to provide upgrade to existing code morning meeting to acoomdate bum team members Meeting Number: 457 231 123 ----------------------------------------------------- to join this meeting ------------------------------------------------------ 1. go to http://domainname.com 2. enter password"
回答1:
You may use regular expressions to capture the numbers also (assuming that there is only numeric characters and spaces in the result, and there is always the "Meeting Number: " prefix):
NSString *text = @"Please plan to attend to provide upgrade to existing code\nmorning meeting to acoomdate bum team members\nMeeting Number: 457 231 123\nTo join this meeting\n\ngo to http://domainname.com\nenter password";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Meeting Number: ([0-9 ]+)" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:text
options:0
range:NSMakeRange(0, [text length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *result = [text substringWithRange:matchRange]; // Here is the result
}
this can be used to parse multiple inputs together since we have a loop here. If the requirements is just to parse one input, replace the code after the declaration of the regex with:
NSTextCheckingResult *match = [regex firstMatchInString:text
options:0
range:NSMakeRange(0, [text length])];
if (match) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *result = [text substringWithRange:matchRange];
}
回答2:
You want NSScanner. It's there specifically for this sort of thing.
NSScanner *scanner = [NSScanner scannerWithString:yourString];
NSString *prefix = @"Meeting number: ";
NSString *numbers;
[scanner scanUpToString:prefix intoString:NULL];
[scanner scanString:prefix intoString:NULL];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&numbers];
// numbers now contains the numbers you want.
(This can be trivially adapted to find any number by doing scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] instead of the two lines with the prefix string.)
回答3:
This will do it (with very little generality):
NSRange promptRange = [bigString rangeOfString:@"Meeting Number: "];
NSRange numberRange = NSMakeRange(promptRange.location + promptRange.length, 11);
NSString *answer = [bigString substringWithRange:numberRange];
回答4:
Might be you can use something like -
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
int numberValue = [[@"Meeting Number: 457 231 123" stringByTrimmingCharactersInSet:nonNumberSet] intValue];
Edit - For all numbers -
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
int numberValue = [[[@"Meeting Number: 457 231 123" componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""] intValue];
回答5:
This should do it, if you want one number:
static int ReadMeetingNumber(NSString * string) {
// ARC:
// locate the line
NSString * const prefix = @"Meeting Number: ";
NSMutableString * meetingNumberLine = nil;
for (NSString * line in [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]) {
if ([line hasPrefix:prefix]) {
meetingNumberLine = [line mutableCopy];
}
}
if (nil == meetingNumberLine) {
return nil;
}
// trim and get int value
[meetingNumberLine deleteCharactersInRange:NSMakeRange(0, prefix.length)];
[meetingNumberLine replaceOccurrencesOfString:@" " withString:@"" options:0 range:NSMakeRange(0, meetingNumberLine.length)];
return meetingNumberLine.intValue;
}
This should do it, if you want to locate the individual numbers:
// ARC:
// Returns an NSArray of NSNumbers
static NSArray * ReadMeetingNumbers(NSString * string) {
// locate the line
NSString * const prefix = @"Meeting Number: ";
NSMutableString * meetingNumberLine = nil;
for (NSString * line in [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]) {
if ([line hasPrefix:prefix]) {
meetingNumberLine = [line mutableCopy];
}
}
if (nil == meetingNumberLine) {
return nil;
}
[meetingNumberLine deleteCharactersInRange:NSMakeRange(0, prefix.length)];
// locate the numbers:
NSMutableArray * results = [NSMutableArray new];
for (NSString * at in [meetingNumberLine componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]) {
const int intValue = at.intValue;
if (0 != intValue) {
[results addObject:[NSNumber numberWithInt:intValue]];
}
}
return [results copy];
}
回答6:
You can accomplish the above task by scanning all the numbers in the above string using the following code.
NSString * inputString = @"Please plan to attend to provide upgrade to existing code morning meeting to acoomdate bum team membersMeeting Number: 457 231 123To join this meeting go tohttp://domainname.comenter password"; // Your input string
NSString * subStr = [inputString substringWithRange:NSMakeRange([inputString rangeOfString:@"Meeting Number:"].location, 27)];
NSString *number = [[subStr componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]; // Scans all numbers from input string
NSLog(@"%@",number); // Your required output printed in console
回答7:
take into string just like
NSString *str = @"Meeting Number: 457 231 123";
seperated by your string
NSArray *theList = [str componentsSeparatedByString:@":"];
NSString *getStr = [theList objectAtIndex:1];
[getStr stringByReplacingOccurrencesOfString:@" " withString:@""];
来源:https://stackoverflow.com/questions/10291881/objective-c-how-to-grab-a-number-after-a-sub-string-from-string