C# 字符串可以';无法解析为UInt32

C# 字符串可以';无法解析为UInt32,c#,unity3d,C#,Unity3d,我正在为Unity中的用户编程登录。我有两个“Text Mesh Pro UGUI”输入字段作为用户名和密码 我需要将用户名(这是一个数字)转换为UInt32来处理用户的登录 但是这个简单的字符串有一个问题→ UInt32解析 代码如下: // Note: I have tried typing different Numbers into the input field but in this case, // I have tried the same as the test_string

我正在为Unity中的用户编程登录。我有两个“Text Mesh Pro UGUI”输入字段作为用户名和密码

我需要将用户名(这是一个数字)转换为UInt32来处理用户的登录

但是这个简单的字符串有一个问题→ UInt32解析

代码如下:

// Note: I have tried typing different Numbers into the input field but in this case, 
// I have tried the same as the test_string (123456)

// This works perfect
string test_string = "123456";

UInt32 test_UInt32 = 0;

if (UInt32.TryParse(test_string, out test_UInt32))
{
    test_UInt32 = UInt32.Parse(test_string);
}

// This never works
UInt32 username_UInt32 = 0;

if (UInt32.TryParse(username.text, out username_UInt32))
{
    username_UInt32 = UInt32.Parse(username.text);
}

// Debugging for me to find the error
Debug.Log(username.text); // Output: 123456
Debug.Log(test_string);   // Output: 123456

Debug.Log(username.text.GetType().ToString());   // Output: System.String
Debug.Log(test_string.GetType().ToString());     // Output: System.String

Debug.Log(username.text.Length.ToString());      // Output: 7
Debug.Log(test_string.Length.ToString());        // Output: 6

// For Testing only => FormatException: Input string was not in a correct format.
username_UInt32 = UInt32.Parse(username.text);

username.text中可能有空格字符
您可以使用此代码删除该空格

username.text = username.text.Trim();
然后解析它

当您使用TryParse方法时,不需要再次使用Parse。把代码改成这个

if (!UInt32.TryParse(username.text, out username_UInt32))
{
    //handle error
}

看,你的长度不一样。您缺少需要调试的内容

  Debug.Log(username.text.Length.ToString());      // Output: 7
  Debug.Log(test_string.Length.ToString());        // Output: 6

UInt32.Parse
方法仅将数字的字符串表示形式转换为其等效的32位无符号整数。一定有一个特殊的角色。空白可以出现在开头和结尾,但不能出现在两者之间。

非常感谢所有这些输入,它现在正在按预期工作

你是对的,有一个隐藏的角色

这就解决了问题:

string clean_string = username.text.Replace("\u200B", "")
有了这个清理过的字符串,解析工作就完美了


你救了我一天。祝你一切顺利

您无法实现
。TryParse()
不仅会告诉您解析是否成功,还会用新的数字填充参数。您一直试图为该参数赋值

    private void button2_Click(object sender, EventArgs e)
    {
        string test_string = textBox1.Text.Trim();
        if (!uint.TryParse(test_string, out uint test_UInt32))
        {
            MessageBox.Show("Invalid Input");
            return;
        }
        if (!UInt32.TryParse(textBox1.Text.Trim(), out uint username_UInt32))
        {
            MessageBox.Show("Invalid Input");
            return;
        }
        Debug.Print($"The input string is {test_string}, the resulting number is {test_UInt32}, of type {test_UInt32.GetType()}");
        //Output  The input string is 1234, the resulting number is 1234, of type System.UInt32
        Debug.Print($"The input string is {textBox1.Text}, the resulting number is {username_UInt32}, of type {username_UInt32.GetType()}");
        //Output  The input string is 1234, the resulting number is 1234, of type System.UInt32
    }

显然,
username.text
中的内容并不表示无符号整数。请注意,UInt32.TryParse/UInt32.Parse不允许字符串中的任何空格或空字符串。另外,
UInt32.TryParse
将解析字符串(如果可以解析),并将结果填充到给定的UInt32变量中;因此,
if
块中的
UInt32.Parse
调用只是多余的,重复UInt32.TryParse已经做过的事情…@elgonzo Hi,感谢您的快速回复。问题是:我无法找出是什么导致我的字符串格式不正确。如果我将字符串记录到控制台,它与我的测试字符串相同。我还将其记录为:“|”+username.text+“|”=>| 123456 |以表明舒尔没有其他字符。我也试过了。Trim()。但还是一样的错误。@elgonzo我就是这么想的。但我什么也找不到。我也不知道还要调试什么。我已经将字符串本身以及它们的类型记录到控制台中。我能做些什么来找出username.text字符串和test_字符串之间的差异吗?长度不同。。。字符串的前面或后面是否有不可打印或零宽度字符(可能只是空终止…)?为了理智起见,username.text.GetType()是否表示它是一个字符串?另外,在
if(UInt32.TryParse(test\u string,out test\u UInt32))
中使用
UInt32.Parse
进行第二次解析也没有意义,因为
TryParse
已经将结果分配给
test\u UInt32
。谢谢。我尝试过.Trim(),但不幸的是仍然是一样的。对username.text字符执行for或foreach操作,然后打印每个字符。然后,您可以找到额外的注意事项:您正在修改诸如“用户名”之类的内容,因此您可能会为您的用户创建登录问题。因为您是在“后端”处理它,所以这里的假设是,您的前端没有验证这样的功能。