.net :怎么vb.net 判断是否数字数字的数位和某个数的个数C#

3264人阅读
今天在写代码时突然想起测试经常用rmation.IsNumeric判断 url参数是否为数字时的这个方法的效率
因为数字是字符串是直接使用的,所以不需要转型,也就没有用tryparse
结果一测试吓一跳,这个方法的效率是如此的低,再测试了下tryparse还不错,正则的也比较差,
没什么技术含量,看结果吧:
先拓展下字符串:
public static class Common
public static bool isNumberic1(this string _string)
if (string.IsNullOrEmpty(_string))
foreach (char c in _string)
if (!char.IsDigit(c))//if(c&'0' || c&'9')//最好的方法,在下面测试数据中再加一个0,然后这种方法效率会搞10毫秒左右
//vb isnumberic
public static bool isNumberic2(this string _string)
return !string.IsNullOrEmpty(_string) && rmation.IsNumeric(_string);
//try parse
public static bool isNumberic3(this string _string)
if (string.IsNullOrEmpty(_string))
int i = 0;
return int.TryParse(_string, out i);
//try catch
public static bool isNumberic4(this string _string)
if (string.IsNullOrEmpty(_string))
try { int.Parse(_string); }
public static bool isNumberic5(this string _string)
return !string.IsNullOrEmpty(_string) && Regex.IsMatch(_string, &^\\d+$&);
测试的代码:
class Program
static void Main(string[] args)
Test(&1234&);
Test(&1234a&);
Test(&a1234&);
Test(null);
static void Test(string str)
bool res1 = false, res2 = false, res3 = false, res4 = false, res5 =
Stopwatch wat = new Stopwatch();
wat.Start();//
for (int i = 1; i & 100000; i++)
res1 = str.isNumberic1();
wat.Stop();
Console.WriteLine(&isDigit
{0}:{1},{2}&, str, wat.ElapsedMilliseconds, res1);
wat.Reset();
wat.Start();
for (int i = 1; i & 100000; i++)
res2= str.isNumberic2();
wat.Stop();
Console.WriteLine(&isNumberic
{0}:{1},{2}&, str, wat.ElapsedMilliseconds, res2);
wat.Reset();
wat.Start();
for (int i = 1; i & 100000; i++)
res3 = str.isNumberic3();
wat.Stop();
Console.WriteLine(&try parse
{0}:{1},{2}&, str, wat.ElapsedMilliseconds, res3);
wat.Reset();
wat.Start();
for (int i = 1; i & 100000; i++)
res4 = str.isNumberic4();
wat.Stop();
Console.WriteLine(&try catch
{0}:{1},{2}&, str, wat.ElapsedMilliseconds, res4);
wat.Reset();
wat.Start();
for (int i = 1; i & 100000; i++)
res5 = str.isNumberic5();
wat.Stop();
Console.WriteLine(&regex
{0}:{1},{2}&, str, wat.ElapsedMilliseconds, res5);
Console.WriteLine();
下面是我本机的测试结果
1234:5,True
isNumberic
1234:21,True
1234:22,True
1234a:5,False
isNumberic
1234a:19,False
a1234:2,False
isNumberic
a1234:16,False
isNumberic
isNumberic
结果:循环判断是否是数字字符效率是最高的
而VisualBasic的方法效率比较低了
顺便测试了下VisualBasic里的left和right方法效率也一样的低,还不及substring的十分之一
所以VisualBasic里虽然有几个方法从名字看来比较好用,实际却比较杯具。
转载请注明出处:&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:138458次
积分:1340
积分:1340
排名:千里之外
原创:19篇
评论:49条[总结]C#判断一个string是否可以为数字,五种解决方案!_.NET教程_编程技术
您的位置: &
& [总结]C#判断一个string是否可以为数字,五种解决方案!
方案一:Try...Catch(执行效率不高)/// &summary&/// 名称:IsNumberic/// 功能:判断输入的是否是数字/// 参数:string oText:源文本/// 返回值: bool true:是 false:否/// &/summary&/// &param name="oText"&&/param&/// &returns&&/returns&private bool IsNumberic(string oText){try&&&&&&&& {int var1=Convert.ToInt32 (oText);&&&&&&&& }catch{}}方案二:正则表达式(推荐)a)using Susing System.Text.RegularEpublic bool IsNumber(String strNumber){Regex objNotNumberPattern=new Regex("[^0-9.-]");Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");return !objNotNumberPattern.IsMatch(strNumber) &&!objTwoDotPattern.IsMatch(strNumber) &&!objTwoMinusPattern.IsMatch(strNumber) &&objNumberPattern.IsMatch(strNumber);}b)public static bool IsNumeric(string value){return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");}public static bool IsInt(string value){return Regex.IsMatch(value, @"^[+-]?\d*$");}public static bool IsUnsign(string value){return Regex.IsMatch(value, @"^\d*[.]?\d*$");}方案三:遍历a)public bool isnumeric(string str){&&& char[] ch=new char[str.Length];&&& ch=str.ToCharArray();&&& for(int i=0;i&ch.Li++)&&& {&&&&&&& if(ch[i]&48 || ch[i]&57)&&&&&&&&&&&&&& }&&&}b)public bool IsInteger(string strIn) {bool bolResult=if(strIn=="") {bolResult=}else {foreach(char Char in strIn) {if(char.IsNumber(Char))else {bolResult=}}}return bolR}c)public static bool isNumeric(string inString){inString=inString.Trim();bool haveNumber=bool haveDot=for(int i=0;i&inString.Li++){if (Char.IsNumber(inString[i])){haveNumber=}else if(inString[i]=='.'){if (haveDot){}else{haveDot=}}else if(i==0){if(inString[i]!='+'&&inString[i]!='-'){}}else{}if(i&20){}}return haveN}}方案四:改写vb的IsNumeric源代码(执行效率不高)//主调函数public static bool IsNumeric(object Expression){&&&&& bool flag1;&&&&& IConvertible convertible1 =&&&&& if (Expression is IConvertible)&&&&& {&&&&&&&&&&& convertible1 = (IConvertible) E&&&&& }&&&&& if (convertible1 == null)&&&&& {&&&&&&&&&&& if (Expression is char[])&&&&&&&&&&& {&&&&&&&&&&&&&&&&& Expression = new string((char[]) Expression);&&&&&&&&&&& }&&&&&&&&&&& else&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&& }&&&&& TypeCode code1 = convertible1.GetTypeCode();&&&&& if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))&&&&& {&&&&&&&&&&& return Utils.IsNumericTypeCode(code1);&&&&& }&&&&& string text1 = convertible1.ToString(null);&&&&& try&&&&& {&&&&&&&&&&& long num2;&&&&&&&&&&& if (!StringType.IsHexOrOctValue(text1, ref num2))&&&&&&&&&&& {&&&&&&&&&&&&&&&&& double num1;&&&&&&&&&&&&&&&&& return DoubleType.TryParse(text1, ref num1);&&&&&&&&&&& }&&&&&&&&&&& flag1 =&&&&& }&&&&& catch (Exception)&&&&& {&&&&&&&&&&& flag1 =&&&&& }&&&&& return flag1;}//子函数// return Utils.IsNumericTypeCode(code1);internal static bool IsNumericTypeCode(TypeCode TypCode){&&&&& switch (TypCode)&&&&& {&&&&&&&&&&& case TypeCode.Boolean:&&&&&&&&&&& case TypeCode.Byte:&&&&&&&&&&& case TypeCode.Int16:&&&&&&&&&&& case TypeCode.Int32:&&&&&&&&&&& case TypeCode.Int64:&&&&&&&&&&& case TypeCode.Single:&&&&&&&&&&& case TypeCode.Double:&&&&&&&&&&& case TypeCode.Decimal:&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&& case TypeCode.Char:&&&&&&&&&&& case TypeCode.SByte:&&&&&&&&&&& case TypeCode.UInt16:&&&&&&&&&&& case TypeCode.UInt32:&&&&&&&&&&& case TypeCode.UInt64:&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&& }&&&&&}&//-----------------//StringType.IsHexOrOctValue(text1, ref num2))internal static bool IsHexOrOctValue(string Value, ref long i64Value){&&&&& int num1;&&&&& int num2 = Value.L&&&&& while (num1 & num2)&&&&& {&&&&&&&&&&& char ch1 = Value[num1];&&&&&&&&&&& if (ch1 == '&')&&&&&&&&&&& {&&&&&&&&&&&&&&&&& ch1 = char.ToLower(Value[num1 + 1], CultureInfo.InvariantCulture);&&&&&&&&&&&&&&&&& string text1 = StringType.ToHalfwidthNumbers(Value.Substring(num1 + 2));&&&&&&&&&&&&&&&&& if (ch1 == 'h')&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& i64Value = Convert.ToInt64(text1, 0x10);&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&& else if (ch1 == 'o')&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& i64Value = Convert.ToInt64(text1, 8);&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&& else&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& throw new FormatException();&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&& if ((ch1 != ' ') && (ch1 != '\u3000'))&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&& num1++;&&&&& }&&&&&}//----------------------------------------------------// DoubleType.TryParse(text1, ref num1);internal static bool TryParse(string Value, ref double Result){&&&&& bool flag1;&&&&& CultureInfo info1 = Utils.GetCultureInfo();&&&&& NumberFormatInfo info3 = info1.NumberF&&&&& NumberFormatInfo info2 = DecimalType.GetNormalizedNumberFormat(info3);&&&&& Value = StringType.ToHalfwidthNumbers(Value, info1);&&&&& if (info3 == info2)&&&&& {&&&&&&&&&&& return double.TryParse(Value, NumberStyles.Any, info2, out Result);&&&&& }&&&&& try&&&&& {&&&&&&&&&&& Result = double.Parse(Value, NumberStyles.Any, info2);&&&&&&&&&&& flag1 =&&&&& }&&&&& catch (FormatException)&&&&& {&&&&&&&&&&& flag1 = double.TryParse(Value, NumberStyles.Any, info3, out Result);&&&&& }&&&&& catch (Exception)&&&&& {&&&&&&&&&&& flag1 =&&&&& }&&&&& return flag1;}方案五: 直接引用vb运行库(执行效率不高)方法: 首先需要添加Visualbasic.runtime的引用&代码中Using Microsoft.&程序中用Information.isnumeric("ddddd");&&Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=292673[点击此处收藏本文]&& 发表于 日 3:53 PM sam 发表于 10:07 PM& IP: 218.70.110.*看来第一种办法最有简单。冰戈 发表于 9:26 AM& IP: 220.163.28.*第一种办法效率不高,建议不用Ofei 发表于 5:43 PM& IP: 219.137.251.*第二种和第三种方法都没有判断数值范围 用""作为参数 判断IsInteger()再用int.Pase()的话定会出错 用"9"作为参数 判断IsNumber() 再用double.Parse()的话也肯定出错 &
( 17:59:01)
( 17:55:20)
( 09:59:06)
( 10:05:04)
( 01:29:49)
( 01:21:08)
( 10:27:55)
( 01:39:35)
相关排行总榜1-15任意排列组合,无重复
找出其在所有排列组合数组中的位置
回复讨论(解决方案)
如果是把组合拆分成1-15的数组还可以理解
找出其在所有排列组合数组中的位置&这句看不懂啊
123&为例子
不同组合的数组
给出231&得到数组位置3
1-15&中个位数可以用01,02表示
好像是有1*2*3*4*&&&&&*15种,你真的要算吗
是+不好意思
好像是有1*2*3*4*&&&&&*15种,你真的要算吗
ok,1-10怎样
好像是有1*2*3*4*&&&&&*15种,你真的要算吗
ok,1-10怎样
首先有个问题,你的排列是否有一定的规律,不然你的问题没有意义,
比如你举的“123”的例子,排列方法有6种,但是顺序又分很多,明白我在说什么吗?
好像是有1*2*3*4*&&&&&*15种,你真的要算吗
ok,1-10怎样
首先有个问题,你的排列是否有一定的规律,不然你的问题没有意义,
比如你举的“123”的例子,排列方法有6种,但是顺序又分很多,明白我在说什么吗?
生成string[]&然后查找,不需要排序,非要的话升序好了
你这问题不就是全排列之后,用一个字符串来找位置么,那问题来了,你的全排列都没有,怎么找位置?我可以说位置是一,也可以说位置是二,因为你的全排列组合根本没出来呢
如果全排列组合已经有了,但没排序,那其实就只有循环找,如果有排序,那就是看快速查找之类的算法
先获得所有组合
&&&&&&&&private&static&IEnumerable&string&&GetAllGroup(string&source,&int&fr,&int&len)
&&&&&&&&&&&&if&(len&==&1)
&&&&&&&&&&&&&&&&return&new&List&string&&{&source.Substring(fr,&1)&};
&&&&&&&&&&&&else
&&&&&&&&&&&&&&&&return&from&sub&in&GetAllGroup(source,&fr&+&1,&len&-&1)
&&&&&&&&&&&&&&&&&&&&&&&from&i&in&Enumerable.Range(0,&sub.Length&+&1)
&&&&&&&&&&&&&&&&&&&&&&&let&first&=&source.Substring(fr,&1)
&&&&&&&&&&&&&&&&&&&&&&&select&sub.Insert(i,&first);
&GetAllGroup(&ABCD&,&0,&4).ToList().ForEach(x&=&&{&Console.WriteLine(x);&});
找出你要的
&&&&&&&&&&&&string&mystr&=&&BDCA&;//你要找的
&&&&&&&&&&&&GetAllGroup(&ABCD&,&0,&4).Select((x,&i)&=&&new&{&i&=&i,&s&=&x&}).ToList().ForEach(x&=&
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&if&(x.s.Equals(mystr))
&&&&&&&&&&&&&&&&&&&&Console.WriteLine(&第{0:D2}个排列是:{1}&,&x.i&+&1,&x.s);
&&&&&&&&&&&&});
注意string是char[]这样的数组
你自己用string[]的话,要修改方法
上面的,楼主说的是15个非雷同元素的组合,你试出结果后截图出来看看吧。
题目本身就有问题,全排序的结果应该作为条件,而不是需要自己去算,如果连全排序都要自己算,那么指定元素的位置我就自己安排了,就安排到第一个?第二个?最后一个,有点搞笑,楼主好好想想你想问什么,还有你能给出一个15个非重复元素的全排列吗?你准备为此花掉多少内存?
你的输入有问题,中间没有空格
1215到底是12&1&5还是12&15
题目本身就有问题,全排序的结果应该作为条件,而不是需要自己去算,如果连全排序都要自己算,那么指定元素的位置我就自己安排了,就安排到第一个?第二个?最后一个,有点搞笑,楼主好好想想你想问什么,还有你能给出一个15个非重复元素的全排列吗?你准备为此花掉多少内存?
我需要的是这生成全组合的方法,然后查找。相同算法生成的全组合,每个字符排列位置是不会变的。小于10的个位数,可以用01表示
题目本身就有问题,全排序的结果应该作为条件,而不是需要自己去算,如果连全排序都要自己算,那么指定元素的位置我就自己安排了,就安排到第一个?第二个?最后一个,有点搞笑,楼主好好想想你想问什么,还有你能给出一个15个非重复元素的全排列吗?你准备为此花掉多少内存?
我需要的是这生成全组合的方法,然后查找。相同算法生成的全组合,每个字符排列位置是不会变的。小于10的个位数,可以用01表示
你要的查找方法,其效率就可以追述到你全排列的生成算法ABC的排列结果可以是:
但这两种结果对于你查询指定排序的位置其效率是不一样的,所以不是单纯谈如何高效的查询所在位置,而是先定下如何有序的进行排列。
看不出这种问题有任何意义
先看一片文章&
里面提到了&字典序的中介数&和&由中介数求序号的算法
123&为例子
不同组合的数组
&排列&&&中介数&&序号
0、123&&00&&&&0*2!&+&0*1!&=&0
1、132&&01&&&&0*2!&+&1*1!&=&1
2、213&&10&&&&1*2!&+&0*1!&=&2
3、231&&11&&&&1*2!&+&1*1!&=&3
4、312&&20&&&&2*2!&+&0*1!&=&4
5、321&&21&&&&2*2!&+&1*1!&=&5
所以&5,6,4,3,2,1,8,9,7,12,15,13,14,11,10
的中介数是&4,5,3,2,1,0,1,1,0,2,4,2,2,1
其序号为&4*14!&+&5*13!&+&3*12!&+&2*11!&+&1*10!&+&0*9!&+&1*8!&+&1*7!&+&0*6!&+&2*5!&+&4*4!&+2*3!&+&2&+&1
当然这是序号是对初态&1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,并按字典法生成的全排列序列而言的
算法复杂度&O(n*n)&远比求取全部排列再查找&O(n!)&要好的多
上面的,楼主说的是15个非雷同元素的组合,你试出结果后截图出来看看吧。
我一开始就提出了这个问题,首先就问了题主有没有一个排序规律,上面只是提供一种解法
C++&有泛型函数&next_permutation
C#&理应也有,只是我初学,不知在哪里能找到
楼主,我真的读你的题目读了好几遍啊
排列都没有定规则,那位置说几都可以啊,这还算什么啊!
还是乖乖组合遍历,快捷方法啥的,你用多核硬件,配合多线程来减少运算时间好了
看上去有点难度服务器维护中.......
我们会尽快恢复,请稍后再试,谢谢!

我要回帖

更多关于 vb.net怎么判断奇偶数 的文章

 

随机推荐