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 901389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:32:52+00:00 2026-05-15T15:32:52+00:00

I am working on .NET project and I am trying to parse only the

  • 0

I am working on .NET project and I am trying to parse only the numeric value from string. For example,

string s = "12ACD";
int t = someparefun(s); 
print(t) //t should be 12

A couple assumptions are

  1. The string pattern always will be number follow by characters.
  2. The number portion always will be either one or two digits value.

Is there any C# predefined function to parse the numeric value from string?

  • 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-05-15T15:32:53+00:00Added an answer on May 15, 2026 at 3:32 pm

    There’s no such function, at least none I know of. But one method would be to use a regular expression to remove everything that is not a number:

    using System;
    using System.Text.RegularExpressions;
    
    int result =
        // The Convert (System) class comes in pretty handy every time
        // you want to convert something.
        Convert.ToInt32(
            Regex.Replace(
                "12ACD",  // Our input
                "[^0-9]", // Select everything that is not in the range of 0-9
                ""        // Replace that with an empty string.
        ));
    

    This function will yield 12 for 12ABC, so if you need to be able to process negative numbers, you’ll need a different solution. It is also not safe, if you pass it only non-digits it will yield a FormatException. Here is some example data:

    "12ACD"  =>  12
    "12A5"   =>  125
    "CA12A"  =>  12
    "-12AD"  =>  12
    ""       =>  FormatException
    "AAAA"   =>  FormatException
    

    A little bit more verbose but safer approach would be to use int.TryParse():

    using System;
    using System.Text.RegularExpression;
    
    public static int ConvertToInt(String input)
    {
        // Replace everything that is no a digit.
        String inputCleaned = Regex.Replace(input, "[^0-9]", "");
    
        int value = 0;
    
        // Tries to parse the int, returns false on failure.
        if (int.TryParse(inputCleaned, out value))
        {
            // The result from parsing can be safely returned.
            return value;
        }
    
        return 0; // Or any other default value.
    }
    

    Some example data again:

    "12ACD"  =>  12
    "12A5"   =>  125
    "CA12A"  =>  12
    "-12AD"  =>  12
    ""       =>  0
    "AAAA"   =>  0
    

    Or if you only want the first number in the string, basically stopping at meeting something that is not a digit, we suddenly also can treat negative numbers with ease:

    using System;
    using System.Text.RegularExpression;
    
    public static int ConvertToInt(String input)
    {
        // Matches the first numebr with or without leading minus.
        Match match = Regex.Match(input, "-?[0-9]+");
    
        if (match.Success)
        {
            // No need to TryParse here, the match has to be at least
            // a 1-digit number.
            return int.Parse(match.Value);
        }
    
        return 0; // Or any other default value.
    }
    

    And again we test it:

    "12ACD"  =>  12
    "12A5"   =>  12
    "CA12A"  =>  12
    "-12AD"  =>  -12
    ""       =>  0
    "AAAA"   =>  0
    

    Overall, if we’re talking about user input I would consider not accepting invalid input at all, only using int.TryParse() without some additional magic and on failure informing the user that the input was suboptimal (and maybe prompting again for a valid number).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do a splash screen using CF.NET but I'm lost in the
I am currently working on one of my college project which requires the implementation
Php / ASP.Net / Jquery I am working on a small blogging website, where
I'm trying to do Isometric 3D in CSS, here is what I have right
So I am very new to web development and have a .NET MVC site
A coworker of mine came across this the other day while we were working
For a better project structure i try to store (build) all my custom jQuery
I have written few test scripts using JUnit 4 and Selenium. I have added
We are required to use a specific library for serialization, that is used like
I have a text file with about 100,000 lines (5 MB), which is updated

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.