amillyard
asked on
c#, asp parse string/text entry -- for any non-numeric characters and strip them from that string
Development Platform : c#, asp.net 2.x, Visual Studio Pro utilising Web Developer, IIS 6, SQL 2005 server
Have an asp form with a text entry box setup.
Want to be able to parse that string/text entry -- for any non-numeric characters and strip them from that string.
How is this done with c# / asp.net please?
Your time and efforts with this enquiry are much apprieated.
Have an asp form with a text entry box setup.
Want to be able to parse that string/text entry -- for any non-numeric characters and strip them from that string.
How is this done with c# / asp.net please?
Your time and efforts with this enquiry are much apprieated.
converted it, here you go
public object StripNonNumeric(object strInput)
{
object iPos;
object sNew;
object iTemp;
strInput = Strings.Trim(strInput);
if (strInput != "") {
iPos = 1;
iTemp = Strings.Len(strInput);
while (iTemp >= iPos) {
if (Information.IsNumeric(Str ings.Mid(s trInput, iPos, 1)) == true) {
sNew = sNew + Strings.Mid(strInput, iPos, 1);
}
iPos = iPos + 1;
}
}
}
and of course you could always use regex, if you dont like that method
http://blogs.msdn.com/ericgu/archive/2005/11/21/495383.aspx
public object StripNonNumeric(object strInput)
{
object iPos;
object sNew;
object iTemp;
strInput = Strings.Trim(strInput);
if (strInput != "") {
iPos = 1;
iTemp = Strings.Len(strInput);
while (iTemp >= iPos) {
if (Information.IsNumeric(Str
sNew = sNew + Strings.Mid(strInput, iPos, 1);
}
iPos = iPos + 1;
}
}
}
and of course you could always use regex, if you dont like that method
http://blogs.msdn.com/ericgu/archive/2005/11/21/495383.aspx
ASKER
thanks for that - apprieated.
now...just cutting and pasting into the asp c# f.cs file -- I am getting the following compile message:
The name 'Strings' does not exist in the current context
now...just cutting and pasting into the asp c# f.cs file -- I am getting the following compile message:
The name 'Strings' does not exist in the current context
Terribly sorry, I converted it without checking.
This works, you will need to add a reference to Microsoft.VisualBasic
public string StripNonNumeric(string strInput)
{
int iPos;
string sNew = string.Empty;
int iTemp;
strInput = strInput.Trim();
if (strInput != "")
{
iPos = 0;
iTemp = strInput.Length-1;
while (iTemp >= iPos)
{
if (Microsoft.VisualBasic.Inf ormation.I sNumeric(s trInput.Su bstring(iP os, 1)) == true)
{
sNew = sNew + strInput.Substring(iPos, 1);
}
iPos = iPos + 1;
}
}
return sNew;
}
This works, you will need to add a reference to Microsoft.VisualBasic
public string StripNonNumeric(string strInput)
{
int iPos;
string sNew = string.Empty;
int iTemp;
strInput = strInput.Trim();
if (strInput != "")
{
iPos = 0;
iTemp = strInput.Length-1;
while (iTemp >= iPos)
{
if (Microsoft.VisualBasic.Inf
{
sNew = sNew + strInput.Substring(iPos, 1);
}
iPos = iPos + 1;
}
}
return sNew;
}
ah, and if you don't want to make the visualbasic reference, here are some other ways to check for numerics
http://www.codeproject.com/useritems/csharp-isnumeric.asp
RedK
http://www.codeproject.com/useritems/csharp-isnumeric.asp
RedK
ASKER
ok -- fyi, I am using c# (not vb)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok -- have added a vb reference.
does this have any affect elsewhere on programming and performance in terms of the asp site...i.e. using c# and vb code within the same page / site etc?
does this have any affect elsewhere on programming and performance in terms of the asp site...i.e. using c# and vb code within the same page / site etc?
ASKER
worked 1st time.
many thanks :-)
many thanks :-)
no there is no impact, but as I say, if you do not like that reference, you can remove it and write your own isnumeric function like this
public bool IsNumeric(string str)
{
try
{
int result = int.Parse(str);
return true;
}
catch
{
return false;
}
}
public bool IsNumeric(string str)
{
try
{
int result = int.Parse(str);
return true;
}
catch
{
return false;
}
}
http://www.aspfree.com/c/a/ASP-Code/Strip-NonNumeric-characters-from-string-function-by-Chad-Scarborough/