从字符串中提取数字,创造数字仅串 [英] Extract numbers from string to create digit only string

查看:115
本文介绍了从字符串中提取数字,创造数字仅串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑一些格式混乱的数据和需要拉出来的数字串。我不知道该怎么做是最好的方法就是。该数字可以是任意长度

I have been given some poorly formatted data and need to pull numbers out of strings. I'm not sure what the best way to do this is. The numbers can be any length.

string a = "557222]]>";
string b = "5100870<br>";



任何想法,我能做些什么,所以我会得到这样的:

any idea what I can do so I'll get this:

a = "557222"
b = "5100870"

感谢

解决方案是为C#抱歉。编辑的问题有标签

Solution is for c# sorry. Edited the question to have that tag

推荐答案

不使用.NET的确切的代码再熟悉不过了。然而,这两种方法是:

Not familiar enough with .NET for exact code. Nonetheless, two approaches would be:


  • 强制转换为整数。如果非数字字符是在年底(即 21389abc ),这是最简单的。

  • 如果您有混合非数字字符(即 1231a23v ),并希望保持的每个的数字,请使用正则表达式 [^ \d] 来替代非数字字符。

  • Cast it as an integer. If the non-digit characters are at the end (i.e. 21389abc), this is the easiest.
  • If you have intermixed non-digit characters (i.e. 1231a23v) and want to keep every digit, use the regex [^\d] to replace non-digit characters.

这篇关于从字符串中提取数字,创造数字仅串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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