如何从字符串中提取数值?
是否可以从字符串中提取数字。例如,我有一个字符串:
my name is shishir and my number is 98890876478
我可以从上面的字符串中提取 98890876478
吗?
或者,如果我的字符串是:
my name is shishir and my number is XXX98890876478XXX
在这种情况下,我可以提取介于 XXX 之间的 `98890876478"。
可以这样做吗?
我从服务器收到一条消息,该消息应采用上述格式,并且我需要进一步操作的数值
编辑:
这是我尝试使用的代码:
NSString *logString = [NSString stringWithFormat:@"%@",theXML];
NSString *digits = [logString stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSLog(@"Message id: %i", [digits intValue]);
输出:
2010-05-21 16:37:07.092 sms[5311:207] OK: message-ID XXX110103468XXX
2010-05-21 16:37:07.851 sms[5311:207] Message id: 2
我认为它返回两个,因为我需要检索“XXX”之间的值。
Is it possible to extract a number from a string. For example, I have a string:
my name is shishir and my number is 98890876478
Can I extract on 98890876478
from the above string?
Or if my string is:
my name is shishir and my number is XXX98890876478XXX
In this condition can I extract `98890876478", which is in between of XXX.
Is it possible to do this?
I am getting a message from a server which should be in the format as above, and I need the numeric value for further operations
Edit:
Here's the code I'm trying to use:
NSString *logString = [NSString stringWithFormat:@"%@",theXML];
NSString *digits = [logString stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSLog(@"Message id: %i", [digits intValue]);
which outputs:
2010-05-21 16:37:07.092 sms[5311:207] OK: message-ID XXX110103468XXX
2010-05-21 16:37:07.851 sms[5311:207] Message id: 2
I think its returning two because size==2. I need to retrieve the value between the "XXX".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你需要的是 NSScanner,它允许你取出字符串的一部分。
例如
I think what you need is NSScanner, this allows you to take out parts of the string.
e.g.
您可以使用正则表达式来实现此目的:您想要匹配
\d+
,即非零的数字序列。相关问题
否则,您可以只需手动浏览字符串中的字符并找到任何连续的数字序列。
You can use regex for this: you want to match
\d+
, that is, a non-zero sequence of digits.Related questions
Otherwise, you can just manually go through the characters in the string and find any contiguous sequence of digits.
自包含解决方案:
处理以下情况:
@"1234"
→@"1234"
@"001234"
→@"001234 "
@"前导文本被删除 001234"
→@"001234"
@"001234 尾随文本被删除"
→@"001234"
@"a0b0c1d2e3f4"
→@"001234"
希望这有帮助!
Self contained solution:
Handles the following cases:
@"1234"
→@"1234"
@"001234"
→@"001234"
@"leading text get removed 001234"
→@"001234"
@"001234 trailing text gets removed"
→@"001234"
@"a0b0c1d2e3f4"
→@"001234"
Hope this helps!
这就是我最终完成的事情
,我完成了。
THIS IS WHAT I FINALLY DONE WITH
And I am done.
stringByTrimmingCharactersInSet 只是删除字符串末尾或开头的字符。
例如:“abc 10 cd”-> 10(会起作用)
例如:“abc 10 a 1”-> “10 a 1”(它不起作用)
使用正则表达式似乎是一个更好的主意。
iPhone sdk - 删除所有字符除了字符串中的数字 0-9
编辑:
检查不接受的答案,而是检查超过 50 票的答案
stringByTrimmingCharactersInSet just remove character at the end or beginning of a string.
ex.: "abc 10 cd" -> 10 (it will work)
ex.: "abc 10 a 1" -> "10 a 1" (it will not work)
Using regex seem a better idea.
iphone sdk - Remove all characters except for numbers 0-9 from a string
EDIT:
Check not the accepted answer but the one with more than 50 votes