-
Feb 22nd, 2006, 06:18 PM
#1
Thread Starter
Fanatic Member
Extract Numbers From String
Here is a method that will extract all of the numbers from a string.
PHP Code:
private string ExtractNumbers(string Expression)
{
string result = null;
char Letter;
for (int i = 0; i < Expression.Length; i++)
{
Letter = Convert.ToChar(Expression.Substring(i, 1));
if (Char.IsNumber(Letter))
{
result += Letter.ToString();
}
}
// MessageBox.Show(result);
return result;
}
It works well.
-Sir Loin
-
Feb 22nd, 2006, 07:15 PM
#2
Re: Extract Numbers From String
Why are you extracting numbers and then putting them into a string? It would seem to make more sense to return an array of integers.
Also, instead of converting each substring to a character, it would be simplier to just access the string like an array for the character (MyString[i] = 'c')
-
Feb 26th, 2006, 04:44 PM
#3
Re: Extract Numbers From String
Don't forget the power of regex:
Code:
static string ExtractNumbers(string Expression)
{
return string.Join(null,System.Text.RegularExpressions.Regex.Split(Expression, "[^\\d]"));
}
-
Feb 8th, 2007, 11:45 AM
#4
Fanatic Member
Re: Extract Numbers From String
I use this - it seems to be faster, however, same as the others, it only works for positive integers:
Code:
private int GetNumericValue(string sVal)
{
int iFirst, iCharVal, iEnd;
int iMult = 1, iRet = 0;
char[] aNumbers = "1234567890".ToCharArray();
iFirst = sVal.IndexOfAny(aNumbers);
iEnd = sVal.LastIndexOfAny(aNumbers);
if (iEnd < 0)
return 0;
string subStr = sVal.Substring(iFirst, iEnd - iFirst + 1);
iEnd = subStr.Length - 1;
while (subStr.Length > 0)
{
iCharVal = int.Parse(subStr[subStr.Length-1].ToString());
iRet += iMult * iCharVal;
iMult *= 10;
if (iEnd <= 0)
break;
subStr = subStr.Substring(0, subStr.Length - 1);
iEnd = subStr.LastIndexOfAny(aNumbers);
subStr = sVal.Substring(iFirst, iEnd + 1);
}
return iRet;
}
Last edited by THEROB; Feb 9th, 2007 at 04:16 AM.
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
-
Feb 9th, 2007, 05:33 AM
#5
Fanatic Member
Re: Extract Numbers From String
This one will return a doube
Code:
private double GetNumericValue(string sVal)
{
int iFirst, iEnd;
char[] aNumbers = "1234567890.-".ToCharArray();
string sRet = null;
bool bMinus = false, bPoint=false;
iFirst = sVal.IndexOfAny(aNumbers);
iEnd = sVal.LastIndexOfAny(aNumbers);
if (iEnd < 0)
return 0;
string subStr = sVal.Substring(iFirst, iEnd - iFirst + 1);
iEnd = subStr.Length - 1;
char cMinus = Convert.ToChar("-");
char cPoint = Convert.ToChar(".");
while (subStr.Length > 0)
{
if (subStr[0].Equals(cMinus))
bMinus = !bMinus;
else if (subStr[0].Equals(cPoint))
{
if (!bPoint)
{
bPoint = true;
sRet += subStr[0];
}
}
else
sRet += subStr[0];
subStr = subStr.Substring(1, subStr.Length-1);
if (subStr.Length <= 0)
break;
iFirst = subStr.IndexOfAny(aNumbers);
subStr = subStr.Substring(iFirst, subStr.Length - iFirst);
}
if (bMinus)
sRet = "-" + sRet;
return double.Parse(sRet);
}
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
-
Mar 13th, 2007, 07:43 AM
#6
Re: Extract Numbers From String
Here is another Method Using Regular Expressions
vb Code:
//using System.Text.RegularExpressions 'global Declartion
Regex Strings =new Regex("[0-9]");
string sString = "0130.1Test";
string sExtracted = Strings.Replace(sString, "");
MessageBox.Show(sExtracted);
Please mark you thread resolved using the Thread Tools as shown
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|