如何获取数字NSString(与数字和字符)? [英] How to get number in NSString(with both numbers and characters)?

查看:162
本文介绍了如何获取数字NSString(与数字和字符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网站获取数据到NSString,如下所示:

I fetch the data from site into NSString like this:

        <td><font color=#ffffff>35</td>
        <td><font color=#ffffff>0</td>
        <td><font color=#ffffff>0</td>
        <td><font color=#ffffff>0</td>
        <td><font color=#ffffff>1</td>
        <td><font color=#ffffff>2</td>
        <td><font color=#ffffff>0</td>
        <td><font color=#ffffff>1</td>
        <td><font color=#ffffff>16</td>
        <td><font color=#ffffff>45.2</td>
        <td><font color=#ffffff>3.153</td>

,我只想转换成NSArray。

and I want to convert into NSArray with numbers only.

我已经尝试过很多类和方法..
floatValue,componentsSeparatedByString,substringWithRange,..所有看起来像可怕和低效率。
有任何基本方法可以解决这个问题吗?

I have been tried many classed and methods.. floatValue, componentsSeparatedByString, substringWithRange, .. all look like terrible and inefficient. Is there any basic method to solve this problem?

谢谢!

推荐答案

解决这个问题的最好的一般方法是使用正则表达式。正则表达式

The best general way to solve this is by using regular expressions. The regular expression

- ?[0-9] * \。?[0-9] *

匹配可选的负号,后跟一些数字,后跟可选的小数点,可选后跟一些其他数字。查看 http://www.regular-expressions.info/floatingpoint.html 关于如何最佳匹配数字与正则表达式的一个很好的讨论。我在这里给出的正则表达式比页面上显示的更简单,但是有点不太稳健。

matches an optional negative sign, followed by some digits, followed by an optional decimal point, optionally followed by some more digits. Take a look at http://www.regular-expressions.info/floatingpoint.html for a nice discussion on how to best match numbers with a regular expression. The regex I give here is simpler than any shown on that page, but it's somewhat less robust.

要匹配Objective-C中的正则表达式,可以使用NSRegularExpression。它有一个名为enumerateMatchesInString的函数,每当它在字符串中匹配时,它将调用一个代码块。下面是一个如何使用它建立一个NSNumbers数组的例子:

To match regular expressions in Objective-C, you can use an NSRegularExpression. It has a function called enumerateMatchesInString that will call a code block every time it gets a match within a string. Here's an example of how you could use that to build up an array of NSNumbers:

NSString *str = @"<td><font color=#ffffff>35</td>\
<td><font color=#ffffff>0</td>\
<td><font color=#ffffff>0</td>\
<td><font color=#ffffff>0</td>\
<td><font color=#ffffff>1</td>\
<td><font color=#ffffff>2</td>\
<td><font color=#ffffff>0</td>\
<td><font color=#ffffff>1</td>\
<td><font color=#ffffff>16</td>\
<td><font color=#ffffff>45.2</td>\
<td><font color=#ffffff>3.153</td>";
NSMutableArray *numbers = [NSMutableArray new];
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"-?[0-9]*\\.?[0-9]*" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];
[regex enumerateMatchesInString:str options:0 range:NSMakeRange(0, [str length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
    NSString *substr = [str substringWithRange:[match range]];
    NSNumber *number = [formatter numberFromString:substr];
    if (number) {
        [numbers addObject:number];
    }
}];
NSLog(@"ARRAY: %@", numbers);

这篇关于如何获取数字NSString(与数字和字符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆