3

我有一个很长的字符串,如下所示

请计划参加以提供对现有代码的升级

早会安排流浪汉团队成员

会议号码:457 231 123


加入此会议

  1. http://domainname.com
  2. 输入密码

现在我想在文本“会议号”之后获取号码,即 457 231 123

请提供任何帮助。谢谢

编辑

假设我有一个字符串

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

7 回答 7

3

你想要NSScanner。它是专门为这种事情而存在的。

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.

scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet](这可以通过做而不是带有前缀字符串的两行来轻松地适应任何数字。)

于 2012-04-24T05:16:36.610 回答
2

您也可以使用正则表达式来捕获数字(假设结果中只有数字字符和空格,并且总是有“会议编号:”前缀):

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
}

这可以用来一起解析多个输入,因为我们这里有一个循环。如果要求只是解析一个输入,请将正则表达式声明后的代码替换为:

NSTextCheckingResult *match = [regex firstMatchInString:text
                                                options:0
                                                  range:NSMakeRange(0, [text length])];
if (match) {
    NSRange matchRange = [match rangeAtIndex:1];
    NSString *result = [text substringWithRange:matchRange];
}
于 2012-04-24T05:28:49.163 回答
0

您可以通过使用以下代码扫描上述字符串中的所有数字来完成上述任务。

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
于 2012-04-24T05:41:35.080 回答
0

可能你可以使用类似的东西 -

NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
int numberValue = [[@"Meeting Number: 457 231 123" stringByTrimmingCharactersInSet:nonNumberSet] intValue];

编辑 - 对于所有数字 -

NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
int numberValue = [[[@"Meeting Number: 457 231 123" componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""] intValue]; 
于 2012-04-24T05:00:40.740 回答
0

这将做到(几乎没有一般性):

NSRange promptRange = [bigString rangeOfString:@"Meeting Number: "];
NSRange numberRange = NSMakeRange(promptRange.location + promptRange.length, 11);
NSString *answer = [bigString substringWithRange:numberRange];
于 2012-04-24T05:02:25.620 回答
0

NSString *str = @"Meeting Number: 457 231 123";

由你的字符串分隔

NSArray *theList = [str componentsSeparatedByString:@":"];
NSString *getStr = [theList objectAtIndex:1];
[getStr stringByReplacingOccurrencesOfString:@" " withString:@""];
于 2012-04-24T05:05:55.820 回答
0

如果您想要一个数字,这应该这样做:

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;
}

如果您想找到各个号码,应该这样做:

// 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];
}
于 2012-04-24T05:10:18.207 回答