从字符串中提取整数

时间:2015-08-19 06:34:38

标签: c# .net

我有个人身份证和行业ID,我想彼此比较,但行业编号前面有零和字母字符。有些数字比其他数字更长。

离。

Personal Number | Industry Number
123             | J000123              
1234            | L001234
12345           | M012345

如果它只是零,我知道如何解决问题。性格引发了我,我将如何实现这一目标?

7 个答案:

答案 0 :(得分:4)

我假设PersonalNumber是Int,而IndustryNumber的类型为String

bool Result = int.Parse(string.Concat(PersonalNumber.Skip(1))) == IndustryNumber;

答案 1 :(得分:2)

对于更新的问题(即行业编号以任意字母开头,后跟由零填充的数字),可能是

// Remove 1st letter, than remove zeros
String number = IndustryNumber.Substring(1).TrimStart('0');

if (number.Equals(personalNumber)) {
  ...
}

答案 2 :(得分:1)

使用RegEx:

var numb = int.Parse(RegEx.Match(industryNumber, @"\d+^").Value);
//Then compare the two ints

答案 3 :(得分:0)

为下面的给定代码创建一个函数

  string a = "str123";
    string b = string.Empty;
    int val;

    for (int i=0; i< a.Length; i++)
    {
        if (Char.IsDigit(a[i]))
            b += a[i];
    }

    if (b.Length>0)
        val = int.Parse(b);

然后调用getInt(mystring)== int

由Dominik回答

    Regex.Replace("J0123", "([A-z]*)", "");

答案 4 :(得分:0)

您可以使用正则表达式删除行业编号的第一部分:

Regex.Replace("J0123", "J0*", "");

结果为&#34; 123&#34;

更新,考虑所有使用此正则表达式的字母:

Regex.Replace("J0123", "[A-Z]0*", "");

答案 5 :(得分:0)

您需要在行业编号和转换后使用substring(): 我假设你使用一个2维的字符串数组。 string [,] myarray。

<access origin="*" />


    code:
<!DOCTYPE html>
<html>
<head>
<!--
Customize this policy to fit your own app's needs. For more guidance, see:
https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
Some notes:
* gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
* Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<script type="text/javascript" src="cordova.js"></script>
<title>Hello World</title>
</head>
<body>
<img src="domain/templates/MediaLoad/adminLoadFiles/e0e8338bb31f8f301cc2332016f05d32.jpg" />
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>

答案 6 :(得分:-1)

假设您有两个用于PersonalId和IndustryID.Result的数组是另一个将保存结果的数组。

for(int i=0; i< PersonalID.Count i++)
{
if(IndustryID[i].Contains(PersonalID[i]))
{
Result[i]=true;
}
else
Result[i]=False;
}