//char的函数能改进 //for循环可以增加并行性 //负号的处理可以尝试改 publicstaticint StringToInt(string s) ...{ int i =0; for (; i < s.Length; i++) if (char.IsWhiteSpace(s[i]) ==false) break; char negative = s[i]; //负数 if (negative =='-'|| negative =='+') i++; long result =0; //改为int或者uint,快很多 for (; i < s.Length; i++) ...{ if (char.IsDigit(s[i]) ==false) //如果能省略,快很多 thrownew FormatException("Input string was not in a correct format."); result =10* result + (s[i] -'0'); if (result >int.MaxValue) //如果能省略,快很多 thrownew OverflowException("Value was either too large or too small for an Int32."); } return (int)(negative =='-'?-result : result); }
功能不强,比系统的略快。但胜在可以修改,都不进行安全判断,可以比系统的快上10倍。 //char的函数能改进//for循环可以增加并行性//负号的处理可以尝试改public static int StringToInt(string s)...{ int i = 0; for (; i s.Length; i++) if (char.IsWhiteSpace