开发者

How to parse numeric value in string representation in Cocoa?

As the title. I tested NSScanner, but it passed some strange strings. (ex :123aaa). Is there any way to convert string<->numb开发者_开发百科er strictly?


You can easily roll your own. Test whether the entire string was scanned, or whether there are additional characters.

NSScanner *scanner = [NSScanner localizedScannerWithString:str];
int i;
if (![scanner scanInt:&i] || [scanner scanLocation] < [str length]) {
    // str contains additional characters
    ...
} else {
    // str contains only an int
    ...
}


NSScanner isn't that high-level. You'll have to validate the string yourself.

One way would be to scan characters up to the set of digits, assert that that failed, then scan the digits, then scan to the end and assert that that failed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜