Friday, June 5, 2015

How to split the integer value from string

I am working asp.net develpment. I want to split the integer value from string value. so simply i found the solution using regular expression and split function


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
 //
 // String containing numbers.
 //
 string sentence = "10 cats, 20 dogs, 40 fish and 1 programmer.";
 //
 // Get all digit sequence as strings.
 //
 string[] digits = Regex.Split(sentence, @"\D+");
 //
 // Now we have each number string.
 //
 foreach (string value in digits)
 {
     //
     // Parse the value to get the number.
     //
     int number;
     if (int.TryParse(value, out number))
     {
  Console.WriteLine(value);
     }
 }
    }
}

1 comment:

  1. 188bet : Bets, Odds, Free Bets & Predictions
    188bet is an online sportsbook that accepts deposits and withdrawals 10bet from leovegas major online sports betting 188bet platforms, including the likes of Football,

    ReplyDelete