Extract Number from String - CSharp System

CSharp examples for System:String Number

Description

Extract Number from String

Demo Code

// Copyright (c) 2012 Computer Technology Solutions, Inc.  ALL RIGHTS RESERVED
using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;/*from   www . j ava2s  .  c  o m*/

public class Main{
        public static int ExtractNumber(this string str)
        {
            var chars = str.ToCharArray();
            var nums = new List<char>();
            foreach (var c in chars)
                if (char.IsNumber(c)) nums.Add(c);

            if (nums.Count > 0) return int.Parse(new string(nums.ToArray()));
            return 0;
        }
}

Related Tutorials