简体   繁体   English

检索字符串值中小数点前的数字值

[英]Retrieving Numeric value before a decimal in a string value

I am working on a routine in C#我正在用 C# 编写一个例程

I have a list of alphanumeric sheet numbers that I would like to retrieve the numbers before the decimal to use in my routine.我有一个字母数字表编号列表,我想检索小数点之前的数字以在我的例程中使用。

FP10.01-->10
M1.01-->1
PP8.01-->8

If possible, how can something like this be achieved as either a string or integer?如果可能,如何将这样的东西作为字符串或整数来实现?

You could use a regex:您可以使用正则表达式:

Regex r = new Regex("([0-9]+)[.]");

string s = "FP10.01";

var result = Convert.ToInt32(r.Match(s).Groups[1].ToString()); //10

To accumulate the resulting elements into a list, you can do something like:要将结果元素累积到列表中,您可以执行以下操作:

List<string> myList = new List<string>(){ "FP10.01","M1.01", "PP8.01"};
List<int> resultSet = 
     myList.Select(e => 
             Regex.Replace(e.Substring(0, e.IndexOf('.')), @"[^\d]", string.Empty))
           .Select(int.Parse)
           .ToList();

This will take each element in myList and in turn, take a substring of each element from index 0 until before the .这将获取myList中的每个元素,然后从索引0. and then replace all the non-numeric data with string.Empty and then finally parse the string element into an int and store it into a list.然后用string.Empty替换所有非数字数据,最后将字符串元素解析为int并将其存储到列表中。

another variant would be:另一种变体是:

List<int> resultSet =
               myList.Select(e => e.Substring(0, e.IndexOf('.')))
                     .Select(e => string.Join(string.Empty, e.Where(char.IsDigit)))
                     .Select(int.Parse)
                     .ToList();

or if you want the elements to be strings then you could do:或者,如果您希望元素为字符串,则可以执行以下操作:

List<string> resultSet =
               myList.Select(e => e.Substring(0, e.IndexOf('.')))
                     .Select(e => string.Join(string.Empty, e.Where(char.IsDigit)))                  
                     .ToList();

To retrieve a single element of type string then you can create a helper function as such:要检索string类型的单个元素,您可以创建一个辅助函数,如下所示:

public static string GetValueBeforeDot(string input){
     return input.Substring(0, input.IndexOf('.'))
                 .Where(char.IsDigit)
                 .Aggregate(string.Empty, (e, a) => e + a);
}

To retrieve a single element of type int then the helper function should be:要检索int类型的单个元素,则辅助函数应为:

public static int GetValueBeforeDot(string input){
       return int.Parse(input.Substring(0, input.IndexOf('.'))
                   .Where(char.IsDigit)
                        .Aggregate(string.Empty, (e, a) => e + a));
}
        string input = "FP10.01";
        string[] _input = input.Split('.');
        string num = find(_input[0]);

        public string find(string input)
        {
            char[] _input = input.ToArray();
            int number;
            string result = null;
            foreach (var item in _input)
            {                
                if (int.TryParse(item.ToString(), out number) == true)
                {
                    result = result + number;
                }
            }

            return result;
        }

This approach removes alphabet characters by replacing them with an empty string.这种方法通过用空字符串替换字母字符来删除字母字符。 Splitting on the '.'在 '.' 上拆分character will leave you with a two element array consisting of numbers at index 0 and after decimal values at index 1.字符将为您留下一个由索引 0 处的数字和索引 1 处的十进制值组成的两元素数组。

string input = "FP10.01";
var result = Regex.Replace(input, @"([A-Za-z]+)", string.Empty).Split('.');
var beforeDecimalNumbers = result[0]; // 10
var afterDecimalNumbers = result[1];  // 01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM