Objective-c 将 NSString 转换为 NSInteger

发布于 2024-12-09 14:50:08 字数 180 浏览 1 评论 0原文


我有一个小问题。当我有一个字符串“3 568 030”并且我使用 [myString intValue];它给我的结果只有 3,问题是我想将整个数字转换为 int/nsinteger。如果有帮助的话,字符串总是只包含数字。我尝试使用replaceoccurrencesofstring(或名称是什么),但不知何故不起作用...
谢谢

I've got this little problem. When I have a string "3 568 030" and I use [myString intValue]; it gives me result just 3, the problem is I want to convert the whole number into int/nsinteger. The string always contains just the number if that's any help. I tried using replaceoccurencesofstring (or what is the name) and it somehow didn't work...
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最终幸福 2024-12-16 14:50:08

执行:

NSString *str = @"3 568 030";

int aValue = [[str stringByReplacingOccurrencesOfString:@" " withString:@""] intValue];
NSLog(@"%d", aValue);

输出

3568030

Do:

NSString *str = @"3 568 030";

int aValue = [[str stringByReplacingOccurrencesOfString:@" " withString:@""] intValue];
NSLog(@"%d", aValue);

output

3568030

昔日梦未散 2024-12-16 14:50:08

这是因为字符串上有空格,您必须首先删除空格,如下所示:

NSString *trimmedString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];

NSInteger *value = [trimmedString intValue];

That is because of the spaces on your string you will have to remove the whitespaces first like this:

NSString *trimmedString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];

NSInteger *value = [trimmedString intValue];
巷子口的你 2024-12-16 14:50:08

我的猜测是您错误地使用了 stringByReplacingOccurencesOfString::

//Remove spaces.
myString = [myString stringByReplacingOccurencesOfString:@" " withString @""];
int myNumber = [myString intValue];

My guess is that you're using stringByReplacingOccurencesOfString:: wrongly.

//Remove spaces.
myString = [myString stringByReplacingOccurencesOfString:@" " withString @""];
int myNumber = [myString intValue];
思念满溢 2024-12-16 14:50:08

首先,使用 : 删除原始字符串中的所有空格,

NSString *trimmedString = [yourOriginalString stringByTrimmingCharactersInSet:
                              [NSCharacterSet whitespaceAndNewlineCharacterSet]];

然后,您可以将其转换为 int/NSInteger。请注意:使用 [myString intValue] 会将字符串转换为 int,但 [myString intValue] 会将其转换为 NSInteger。

First, remove all the whitespaces in your original string using :

NSString *trimmedString = [yourOriginalString stringByTrimmingCharactersInSet:
                              [NSCharacterSet whitespaceAndNewlineCharacterSet]];

Then, you can convert it to an int/NSInteger. beware: using [myString intValue] will cast your string to an int, but [myString integerValue] will cast it to a NSInteger.

鲜肉鲜肉永远不皱 2024-12-16 14:50:08
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:@"42"];
[f release];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:@"42"];
[f release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文