1

I am working on a routine in 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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
blkscrpn5
  • 11
  • 5
  • https://stackoverflow.com/questions/4734116/find-and-extract-a-number-from-a-string check this, may have an idea – Hari Jan 26 '18 at 01:03
  • Try putting the each alphanumeric string in a list, and convert all possible items (the numbers) to integers. Then read the adjacent integers and output them in one string. – Rohan Shetty Jan 26 '18 at 01:04

4 Answers4

1

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
SBFrancies
  • 3,987
  • 2
  • 14
  • 37
0

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 . 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.

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:

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:

public static int GetValueBeforeDot(string input){
       return int.Parse(input.Substring(0, input.IndexOf('.'))
                   .Where(char.IsDigit)
                        .Aggregate(string.Empty, (e, a) => e + a));
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0
        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;
        }
Tuan Zaidi
  • 343
  • 1
  • 14
0

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.

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
Matthew Thurston
  • 720
  • 5
  • 22