开发者

How to scan this string?

Suppose I have a string like this: Conditions for Paris, FR at 10:29 pm CET.

I'm wanting to grab the time: 10:29.

I know how to scan a string into another string up to a certain point, like this:

NSString *something;
NSScanner *scanner = [NSScanner scannerWithString:@"Sometest_something"];
[scanner scanUpToString:@"_" intoString:&something];

And the result will be Sometest.

However, this doesn't seem to help me get the time string I'm looking for. Any ideas? I'm sure it's q开发者_如何转开发uite simple.

Update: I also need to have am/pm as a separate string. For getting the time, Caleb's answer works perfectly. But I still haven't figured out how to get am/pm. Any ideas?

Yep, here we go, figured it out. :)

NSScanner *scanner = [NSScanner scannerWithString:timestring];
NSString *ampm;
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet uppercaseLetterCharacterSet] intoString:&ampm];


It's not clear from your question, but I'm guessing that the "Paris, FR" part of the string might change but the rest of the string (obviously not including the time) is constant? If so, you could:

[scanner scanUpToString:@" at " intoString:nil];
[scanner scanString:@" at " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&something];

Edit: Come to think of it, if you know that there are no digits before the time, you could just use -scanUpToCharactersFromSet: for the whole thing:

[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&something];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜