Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:28:46+00:00 2026-06-15T11:28:46+00:00

i have this C# code that i found and then improved upon for my

  • 0

i have this C# code that i found and then improved upon for my needs, but now i would like to make it work for all numeric data types.

    public static int[] intRemover (string input)
    {
        string[] inputArray = Regex.Split (input, @"\D+");
        int n = 0;
        foreach (string inputN in inputArray) {
            if (!string.IsNullOrEmpty (inputN)) {
                n++;
            }
        }
        int[] intarray = new int[n];
        n = 0;
        foreach (string inputN in inputArray) {
            if (!string.IsNullOrEmpty (inputN)) {
                intarray [n] = int.Parse (inputN);
                n++;
            }
        }
        return intarray;
    }

This works well for trying to extract whole number integers out of strings but the issue that i have is that the regex expression i am using is not setup to account for numbers that are negative or numbers that contain a decimal point in them. My goal in the end like i said is to make a method out of this that works upon all numeric data types. Can anyone help me out please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:28:47+00:00Added an answer on June 15, 2026 at 11:28 am

    You can match it instead of splitting it

    public static decimal[] intRemover (string input)
    {
        return Regex.Matches(input,@"[+-]?\d+(\.\d+)?")//this returns all the matches in input
                    .Cast<Match>()//this casts from MatchCollection to IEnumerable<Match>
                    .Select(x=>decimal.Parse(x.Value))//this parses each of the matched string to decimal
                    .ToArray();//this converts IEnumerable<decimal> to an Array of decimal
    }
    

    [+-]? matches + or - 0 or 1 time

    \d+ matches 1 to many digits

    (\.\d+)? matches a (decimal followed by 1 to many digits) 0 to 1 time


    Simplified form of the above code

        public static decimal[] intRemover (string input)
        {
            int n=0;
            MatchCollection matches=Regex.Matches(input,@"[+-]?\d+(\.\d+)?");
            decimal[] decimalarray = new decimal[matches.Count];
    
            foreach (Match m in matches) 
            {
                    decimalarray[n] = decimal.Parse (m.Value);
                    n++;
            }
            return decimalarray;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that works in a unit test but doesn't work when
I currently have this useful code that I found elsewhere on StackOverflow: form.DrawToBitmap(bmp, new
I have this Objective-C code that I found somewhere. Honestly, I don't understand a
I have found this line of code in a game that I study int
I have found this line of MatLab code on the internet that displays a
I have this code that runs but never stops. class A { public static
I have this code that where I would normally use one line: if (tableView
I have a piece of code that looks like this: downloadsByExtensionCount = defaultdict(int) downloadsByExtensionList
I have this code that is an example I found. In this code, I
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.