Remove non numeric characters from string and cast numbers as an int into an array

Viewed 1377

I have a string: "ABD1254AGSHF56984,5845fhfhjekf!54685" and I want to loop through each character in this string and find any non numeric character and remove the characters until the next numeric character. i.e. remove all the non numeric characters from my string but these should be an indiciator that the numbers are seperate.

Output as an integer array:

1254
56984
5845
54685

These should be put into an array and converted as integers.

My attempt below but this just puts all the numbers as one rather than splitting them up based on the non numeric characters:

var input = "ABD1254AGSHF56984,5845fhfhjekf!54685";
var numeric = new String(input.Where(char.IsDigit).ToArray());

//This is the output of my attempt: 125456984584554685
4 Answers

You can use Regex to split up your numbers and letters,

string line = "ABD1254AGSHF56984,5845fhfhjekf!54685";

// This splits up big string into a collection of strings until anything other than a character is seen.
var words = Regex.Matches(line, @"[a-zA-Z]+"); 

// This gives you a collection of numbers... 
var numbers = Regex.Matches(line, @"[0-9]+");
foreach (Match number in numbers)
{
    Console.WriteLine(number.Value);
}

// prints
1254
56984
5845
54685

Regex Documentation should be read before implementation for better understanding.

If you can use the .NET Framework, there's a great class called System.Text.RegularExpressions.Regex that can be your new best friend.

var input = "ABD1254AGSHF56984,5845fhfhjekf!54685";
var numeric = Regex.Replace(input, @"[^\d]+", "\n").Trim();

What the above method is doing is it is looking for one or more non-decimal characters and replacing it with a carriage return (\n). The Trim() ensures that it removes any leading or trailing carriage returns.

Output:

1254
56984
5845
54685

There is no need for black magic to solve such a problem. You can solve it with a for-loop. It's been there since the begining of programming and is designed for such stuff ;)

  1. check IF the character is a number. Then start collecting the digits

  2. ELSE you hit a boundary and you can convert the collected digits to a single int and clear your temporal digit storage:

the only tricky thing here is that if you have a number at the end (where there is no non-numeric character as boundary afterwards) you need to check whether you hit the final boundary.

var input = "ABD1254AGSHF56984,5845fhfhjekf!54685";
string separateNumber = "";
List<int> collection = new List<int>();

for (int i = 0; i < input.Length; i++)
{
    if (Char.IsDigit(input[i]))
    {
        separateNumber += input[i];
        
        if (i == input.Length -1) // ensures that the last number is caught
        {
            collection.Add(Convert.ToInt32(separateNumber));
        }
    }
    else if (string.IsNullOrEmpty(separateNumber) == false)
    {
        collection.Add(Convert.ToInt32(separateNumber));
        separateNumber = "";
    }
}

You're almost there, all you need is to not pass the results of your LINQ query to a new String() instance. This should work:

var numeric = input.Where(char.IsDigit).ToArray();
Related