Objective C:如何从字符串中获取子字符串后的数字。

时间:2023-01-28 19:36:24

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

会议编号:457231 123。


To join this meeting

  1. go to http://domainname.com
  2. 去http://domainname.com
  3. enter password
  4. 输入密码

Now i want to grab number after the text "Meeting Number" i.e. 457 231 123

现在我想在文本“Meeting number”(即457 231 123)之后抓取number

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"

7 个解决方案

#1


2  

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:

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

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


3  

You want NSScanner. It's there specifically for this sort of thing.

你想要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.

(This can be trivially adapted to find any number by doing scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] instead of the two lines with the prefix string.)

(这可以通过执行scanUpToCharactersFromSet:[NSCharacterSet decimaldigit字符集]而不是使用前缀字符串的两行来进行调整以查找任何数字。)

#3


0  

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


0  

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


0  

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


0  

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


0  

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:@""];

#1


2  

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:

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

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


3  

You want NSScanner. It's there specifically for this sort of thing.

你想要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.

(This can be trivially adapted to find any number by doing scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] instead of the two lines with the prefix string.)

(这可以通过执行scanUpToCharactersFromSet:[NSCharacterSet decimaldigit字符集]而不是使用前缀字符串的两行来进行调整以查找任何数字。)

#3


0  

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


0  

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


0  

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


0  

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


0  

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:@""];