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

时间:2022-03-02 16:00:28

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?

我可以从上面的字符串中提取98890876478吗?

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.

在这种情况下,我可以提取‘98890876478’,在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".

我想它会返回2,因为size= 2。我需要检索“XXX”之间的值。

5 个解决方案

#1


7  

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

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

e.g.

如。

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

#2


1  

You can use regex for this: you want to match \d+, that is, a non-zero sequence of digits.

您可以使用regex进行此操作:您希望匹配\d+,也就是说,一个非零的数字序列。

Related questions


Otherwise, you can just manually go through the characters in the string and find any contiguous sequence of digits.

否则,您只需手动遍历字符串中的字符并查找任何连续的数字序列。

#3


1  

Self contained solution:

自包含的解决方案:

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

Handles the following cases:

处理以下情况:

  • @"1234"@"1234"
  • @“1234”→@“1234”
  • @"001234"@"001234"
  • @“001234”→@“001234”
  • @"leading text get removed 001234"@"001234"
  • @“领先的文本删除001234”→@“001234”
  • @"001234 trailing text gets removed"@"001234"
  • @“001234后文本被删除”→@“001234”
  • @"a0b0c1d2e3f4"@"001234"
  • @“a0b0c1d2e3f4”→@“001234”

Hope this helps!

希望这可以帮助!

#4


0  

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.

我完成了。

#5


0  

stringByTrimmingCharactersInSet just remove character at the end or beginning of a string.

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

ex.: "abc 10 cd" -> 10 (it will work)

例:“abc 10 cd”-> 10(可以)

ex.: "abc 10 a 1" -> "10 a 1" (it will not work)

例:“abc 10 a 1”->“10 a 1”(不能用)

Using regex seem a better idea.

使用regex似乎是个更好的主意。

iphone sdk - Remove all characters except for numbers 0-9 from a string

iphone sdk——除去字符串中0-9之外的所有字符

EDIT:

编辑:

Check not the accepted answer but the one with more than 50 votes

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

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

#1


7  

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

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

e.g.

如。

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

#2


1  

You can use regex for this: you want to match \d+, that is, a non-zero sequence of digits.

您可以使用regex进行此操作:您希望匹配\d+,也就是说,一个非零的数字序列。

Related questions


Otherwise, you can just manually go through the characters in the string and find any contiguous sequence of digits.

否则,您只需手动遍历字符串中的字符并查找任何连续的数字序列。

#3


1  

Self contained solution:

自包含的解决方案:

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

Handles the following cases:

处理以下情况:

  • @"1234"@"1234"
  • @“1234”→@“1234”
  • @"001234"@"001234"
  • @“001234”→@“001234”
  • @"leading text get removed 001234"@"001234"
  • @“领先的文本删除001234”→@“001234”
  • @"001234 trailing text gets removed"@"001234"
  • @“001234后文本被删除”→@“001234”
  • @"a0b0c1d2e3f4"@"001234"
  • @“a0b0c1d2e3f4”→@“001234”

Hope this helps!

希望这可以帮助!

#4


0  

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.

我完成了。

#5


0  

stringByTrimmingCharactersInSet just remove character at the end or beginning of a string.

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

ex.: "abc 10 cd" -> 10 (it will work)

例:“abc 10 cd”-> 10(可以)

ex.: "abc 10 a 1" -> "10 a 1" (it will not work)

例:“abc 10 a 1”->“10 a 1”(不能用)

Using regex seem a better idea.

使用regex似乎是个更好的主意。

iphone sdk - Remove all characters except for numbers 0-9 from a string

iphone sdk——除去字符串中0-9之外的所有字符

EDIT:

编辑:

Check not the accepted answer but the one with more than 50 votes

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

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