首页 > c# > 字符串无法解析为UInt32

字符串无法解析为UInt32 (String can't be parsed into UInt32)

2019-03-02 c#unity3d

问题

我正在为Unity中的用户编写一个Login。我有2个“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);

解决方法

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

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

这解决了这个问题:

string clean_string = username.text.Replace("\u200B", "")

通过这个清理过的字符串,解析工作完美。

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

问题

I am programming a Login for users in Unity. I have 2 "Text Mesh Pro UGUI" input fields for a username and a password.

I need to convert the username (which is a number) into an UInt32 to handle the Login of the User.

But there is a Problem with this simple string → UInt32 parsing.

This is the code:

// 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);

解决方法

Thanks a lot for all this input, it is working as expected now!

You were right, there was a hidden character.

This solved the problem:

string clean_string = username.text.Replace("\u200B", "")

With this cleaned up string, the parsing worked perfectly.

You saved my day. Wish you all the best!

相似信息