Parsing numbers from text

  • Thread starter Thread starter Brian Bender
  • Start date Start date
B

Brian Bender

If I have a string of "1t2e3s4t5i6n7g8"

I want to return a string of "12345678"

Is there a String.Format syntax to this or can it be returned using int.parse?
 
Try this

string s = "1t2e3s4t5i6n7g8"

char[] ch = s.ToCharArray()
string i = null
foreach(char c in ch

tr

i += Int32.Parse(c.ToString() )


catch{


int iData = Convert.ToInt32(i);
 
Brian,

Or even shorter, without the overheat of having to have exceptions
being thrown as soon as it hits a character.

string number = "";
string mixed = "1a2b3c4d5";
foreach(char c in mixed)
{
if( char.IsNumber(c) )
number += c.ToString();
}

HTH,

//Andreas
 
Back
Top