C# char.isnumber(';';)在";“美国”;场所

C# char.isnumber(';';)在";“美国”;场所,c#,.net,winforms,C#,.net,Winforms,我正在制作使用g代码的windows窗体应用程序。 基本上,输入是如下所示的字符串: X32.2Y47Z100.5 为了首先检索每个数字,我必须找出表示数字的单个子字符串的长度 在我的加载事件中,我有以下内容: Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpec

我正在制作使用g代码的windows窗体应用程序。 基本上,输入是如下所示的字符串:

X32.2Y47Z100.5

为了首先检索每个数字,我必须找出表示数字的单个子字符串的长度

在我的加载事件中,我有以下内容:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
在这些行之后,区域设置本身具有值“en US” 但以下两个结果都返回false

char.IsNumber('.');
char.IsNumber(',');

此外,当字符串包含“.”或“,”时,convert.toDouble()也会失败。

问题在于,您希望字符
被视为数字,而不是数字。它们仅在与实际数字一起使用时才被视为有效数字的一部分,并且仅当它们的位置根据当前区域性是正确的时才被视为有效数字的一部分

您可以使用
RegEx
string.split
方法拆分x、y和z字符上的字符串,然后可以使用
double.TryParse
将拆分部分转换为双精度:

private static void Main()
{
    string input = "X32.2Y47Z100.5";

    string[] inputParts = input.Split(new[] {'X', 'Y', 'Z'}, 
        StringSplitOptions.RemoveEmptyEntries);

    if (inputParts.Length == 3)
    {
        double x, y, z;
        double.TryParse(inputParts[0], out x);
        double.TryParse(inputParts[1], out y);
        double.TryParse(inputParts[2], out z);
        Console.WriteLine($"The values are: x = {x}, y = {y}, z = {z}");
    }
    else
    {
        Console.WriteLine("Input was not in a valid format.");
    }

    Console.WriteLine("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}
输出

  • 使用正则表达式,将所有字母替换为“;”

  • 将结果拆分为“;”

  • 删除所有空条目

    YourInputStringArray =  YourInputStringArray .Where(x => !string.IsNullOrEmpty(x)).ToArray();
    
  • 将其转换为双精度

    foreach(var inputString in YourInputStringArray)
    {
       if(double.TryParse(inputString, out var result)){
           // Here is your double value
       }
    }
    

  • 为什么您希望它返回true?您希望
    代表什么数字?此外,您应该使用double.Parse或TryParse而不是Convert.todouble。您在这里试图解决的实际问题是什么。我想这是regex的一个例子
    YourInputStringArray =  YourInputStringArray .Where(x => !string.IsNullOrEmpty(x)).ToArray();
    
    foreach(var inputString in YourInputStringArray)
    {
       if(double.TryParse(inputString, out var result)){
           // Here is your double value
       }
    }