如何从字符串中提取数值?

发布于 2024-09-02 13:23:36 字数 849 浏览 3 评论 0原文

是否可以从字符串中提取数字。例如,我有一个字符串:

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 技术交流群。

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

发布评论

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

评论(5

铜锣湾横着走 2024-09-09 13:23:36

我认为你需要的是 NSScanner,它允许你取出字符串的一部分。

例如

int n;
NSScanner * scanner = [[NSScanner alloc] initWithString:@"Your string 1234"];
[scanner scanInt:&n];

I think what you need is NSScanner, this allows you to take out parts of the string.

e.g.

int n;
NSScanner * scanner = [[NSScanner alloc] initWithString:@"Your string 1234"];
[scanner scanInt:&n];
云朵有点甜 2024-09-09 13:23:36

您可以使用正则表达式来实现此目的:您想要匹配 \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.

怪我太投入 2024-09-09 13:23:36

自包含解决方案:

+ (NSString *)extractNumberFromText:(NSString *)text
{
  NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
  return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
}

处理以下情况:

  • @"1234"@"1234"
  • @"001234"@"001234 "
  • @"前导文本被删除 001234"@"001234"
  • @"001234 尾随文本被删除"@"001234"
  • @"a0b0c1d2e3f4"@"001234"

希望这有帮助!

Self contained solution:

+ (NSString *)extractNumberFromText:(NSString *)text
{
  NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
  return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
}

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!

江南烟雨〆相思醉 2024-09-09 13:23:36

这就是我最终完成的事情

NSString *newString = [[theXML componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
----------------------------------------

NSString *newStr = [newString substringWithRange:NSMakeRange(1, [newString length]-1)];
----------------------------------------

,我完成了。

THIS IS WHAT I FINALLY DONE WITH

NSString *newString = [[theXML componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
----------------------------------------

NSString *newStr = [newString substringWithRange:NSMakeRange(1, [newString length]-1)];
----------------------------------------

And I am done.

演多会厌 2024-09-09 13:23:36

stringByTrimmingCharactersInSet 只是删除字符串末尾或开头的字符。

例如:“abc 10 cd”-> 10(会起作用)

例如:“abc 10 a 1”-> “10 a 1”(它不起作用)

使用正则表达式似乎是一个更好的主意。

iPhone sdk - 删除所有字符除了字符串中的数字 0-9

编辑:

检查不接受的答案,而是检查超过 50 票的答案

NSString * number = @"(555) 555-555 Office";
NSString * strippedNumber = [number stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [number length])];

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

NSString * number = @"(555) 555-555 Office";
NSString * strippedNumber = [number stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [number length])];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文