[C#] public static bool IsNumber( string s, int index ); 指示指定字符串中位于指定位置的字符是否属于数字类别。using System;public class IsNumberSample { public static void Main() { string str = "non-numeric"; Console.WriteLine(Char.IsNumber('8')); // Output: "True" Console.WriteLine(Char.IsNumber(str, 3)); // Output: "False" } }
using Microsoft.VisualBasic;呵呵
you can validate your data with js:isNan() --- is not a numer.
错了,刚刚开MSDN才知道VB中的这个函数是以一个Object为参数的,下面的函数就凑合着使用吧:) bool IsNumeric(Object ob){ string pattern = @"^\s*\d*(?:.\d+)\s*$"; Regex r = new Regex(pattern,RegexOptions.IgnoreCase); Match m = r.Match(ob.ToString()); return m.Success; } ps. solomon2002(solomon)的没有处理小数点问题,我的也可能不完善,但可以处理大多数状况:)
示例 public class IsNumberSample { public static void Main() { string str = "non-numeric"; Console.WriteLine(Char.IsNumber('8')); // Output: "True" Console.WriteLine(Char.IsNumber(str, 3)); // Output: "False" } }
ft~~IsNumeric对所有小数取false??try:bool IsNumeric(object ob){ string pattern = @"^(0|([1-9]\d*))$"; Regex r = new Regex(pattern); Match m = r.Match(ob.ToString()); return m.Success; }
private bool IsNumeric(String s)
{
if(s.Equals(""))return false;
System.Text.StringBuilder sb=new StringBuilder(s);
for(int i=0;i<sb.Length;i++)
{
if(!Char.IsNumber(sb[i]))
{
return false;
}
}
public static bool IsNumber(
string s,
int index
);
指示指定字符串中位于指定位置的字符是否属于数字类别。using System;public class IsNumberSample {
public static void Main() {
string str = "non-numeric"; Console.WriteLine(Char.IsNumber('8')); // Output: "True"
Console.WriteLine(Char.IsNumber(str, 3)); // Output: "False"
}
}
using Microsoft.VisualBasic;呵呵
string pattern = @"^\s*\d*(?:.\d+)\s*$";
Regex r = new Regex(pattern,RegexOptions.IgnoreCase);
Match m = r.Match(ob.ToString());
return m.Success;
}
ps. solomon2002(solomon)的没有处理小数点问题,我的也可能不完善,但可以处理大多数状况:)
public class IsNumberSample {
public static void Main() {
string str = "non-numeric"; Console.WriteLine(Char.IsNumber('8')); // Output: "True"
Console.WriteLine(Char.IsNumber(str, 3)); // Output: "False"
}
}
---------------
for(int i=0;i<sb.Length;i++)
{
if(!Char.IsNumber(sb[i]))
{
return false;
}
}
===false
string pattern = @"^(0|([1-9]\d*))$";
Regex r = new Regex(pattern);
Match m = r.Match(ob.ToString());
return m.Success;
}