你好,我正在嘗試將字符串轉換爲整數。將字符串轉換爲int中的錯誤#
下面的代碼顯示了我嘗試將字符串轉換爲整數的一部分。
if (other.gameObject.CompareTag("PickUp"))
{
if (checkpointboolean == false)
{
string pickupName = other.ToString(); //other = Pickup4
//Remove first 6 letters thus remaining with '4'
string y = pickupName.Substring(6);
print(y); // 4 is being printed
int x = 0;
int.TryParse(y, out x);
print (x); // 0 is being printed
我也試過下面的代碼和,而不是「0」我收到以下錯誤:
if (other.gameObject.CompareTag("PickUp"))
{
if (checkpointboolean == false)
{
//Get Object name ex: Pickup4
string pickupName = other.ToString();
//Remove first 6 letters thus remaining with '4'
string y = pickupName.Substring(6);
print(y);
int x = int.Parse(y);
FormatException: Input string was not in the correct format System.Int32.Parse (System.String s)
如何確定你是沒有charaters * *後的4?嘗試打印'y.Length' ... –
你必須逐步調試它與斷點 –
這似乎告訴你,你的字符串包含無效字符。你確定你在該子串中只有4個?如果你有一個調試器試圖看看這個值,那麼丟失它然後改變使用print(「[」+ y +「]」);看看是否有其他人物 – Steve