我想查找字符串是否具有指数格式。目前我正在检查如下。
var s = "1.23E+04";
var hasExponential = s.Contains("E");
但我知道这不是正确的方法。那么有人可以指导正确和快速的方式来实现我的要求吗?
答案 0 :(得分:6)
如果您还想确保它确实是一个数字,而不仅仅是一个带有' E'在其中,也许这样的功能可能会有所帮助。逻辑仍然很简单。
private bool IsExponentialFormat(string str)
{
double dummy;
return (str.Contains("E") || str.Contains("e")) && double.TryParse(str, out dummy);
}
答案 1 :(得分:1)
尝试使用正则表达式?
string s = "1.23E+04";
string pattern = @"^\d{1}.\d+(E\+)\d+$";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
bool hasExponential = rgx.IsMatch(s);
答案 2 :(得分:0)
您可以随时在e
上分开。或者使用double.TryParse
。两者都很好。 (我敢打赌ParseExponential2
对于有效的ParseExponential1
更快,而对于无效的public static void _Main(string[] args)
{
string[] exponentials = new string[] { "1.23E+4", "1.23E+04", "1.23e+4", "1.23", "1.23e+4e+4", "abce+def", "1.23E-04" };
for (int i = 0; i < exponentials.Length; i++)
Console.WriteLine("Input: {0}; Result 1: {1}; Result 2: {2}; Result 3: {3}", exponentials[i], (ParseExponential1(exponentials[i]) ?? 0), (ParseExponential2(exponentials[i]) ?? 0), (ParseExponential3(exponentials[i]) ?? 0));
}
public static double? ParseExponential1(string input)
{
if (input.Contains("e") || input.Contains("E"))
{
string[] inputSplit = input.Split(new char[] { 'e', 'E' });
if (inputSplit.Length == 2) // If there were not two elements split out, it's an invalid exponential.
{
double left = 0;
int right = 0;
if (double.TryParse(inputSplit[0], out left) && int.TryParse(inputSplit[1], out right) // Parse the values
&& (left >= -5.0d && left <= 5.0d && right >= -324) // Check that the values are within the range of a double, this is the minimum.
&& (left >= -1.7d && left <= 1.7d && right <= 308)) // Check that the values are within the range of a double, this is the maximum.
{
double result = 0;
if (double.TryParse(input, out result))
return result;
}
}
}
return null;
}
public static double? ParseExponential2(string input)
{
if (input.Contains("e") || input.Contains("E"))
{
double result = 0;
if (double.TryParse(input, out result))
return result;
}
return null;
}
public static double? ParseExponential3(string input)
{
double result = 0;
if (double.TryParse(input, out result))
return result;
return null;
}
可以更快。)
ParseExponential1
如果ParseExponential2
,ParseExponential3
或null
返回ParseExponential3
,则无效。这也允许您同时解析它并获得指示的值。
1.23
的唯一问题是,如果它们不是指数的,它也会返回有效数字。 (即,对于1.23
案例,它将返回double
。)
您还可以删除Regex
范围的支票。他们只是为了完整。
另外,为了证明这一点,我只是使用下面的代码运行基准测试,而nevermoi在另一个答案中的500ms
选项使exponentials
在ParseExponential1
上运行了100,000次}数组,547ms
选项使用ParseExponential2
,317ms
选项346ms
。 sstan的方法花了ParseExponential3
。最后,最快的是我在134ms
的{{1}}方法。
string[] exponentials = new string[] { "1.23E+4", "1.23E+04", "1.23e+4", "1.23", "1.23e+4e+4", "abce+def", "1.23E-04" };
Stopwatch sw = new Stopwatch();
sw.Start();
for (int round = 0; round < 100000; round++)
for (int i = 0; i < exponentials.Length; i++)
ParseExponential1(exponentials[i]);
sw.Stop();
Console.WriteLine("Benchmark 1 (ParseExponential1) complete: {0}ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int round = 0; round < 100000; round++)
for (int i = 0; i < exponentials.Length; i++)
ParseExponential2(exponentials[i]);
sw.Stop();
Console.WriteLine("Benchmark 2 (ParseExponential2) complete: {0}ms", sw.ElapsedMilliseconds);
sw.Reset();
string pattern = @"^\d{1}.\d+(E\+)\d+$";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
sw.Start();
for (int round = 0; round < 100000; round++)
for (int i = 0; i < exponentials.Length; i++)
rgx.IsMatch(exponentials[i]);
sw.Stop();
Console.WriteLine("Benchmark 3 (Regex Parse) complete: {0}ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int round = 0; round < 100000; round++)
for (int i = 0; i < exponentials.Length; i++)
IsExponentialFormat(exponentials[i]);
sw.Stop();
Console.WriteLine("Benchmark 4 (IsExponentialFormat) complete: {0}ms", sw.ElapsedMilliseconds);
sw.Start();
for (int round = 0; round < 100000; round++)
for (int i = 0; i < exponentials.Length; i++)
ParseExponential3(exponentials[i]);
sw.Stop();
Console.WriteLine("Benchmark 5 (ParseExponential3) complete: {0}ms", sw.ElapsedMilliseconds);
并添加了以下方法:
private static bool IsExponentialFormat(string str)
{
double dummy;
return (str.Contains("E") || str.Contains("e")) && double.TryParse(str, out dummy);
}