簡體   English   中英

將數字字符串轉換為int時,它返回字符串的Ascii代碼嗎?

[英]When converting a string of numbers to int , it returns Ascii code for the string?

我有以下格式的字符串:“ p100”“ p231”“ p000”。

這些字符串是按鈕名稱,最后3個字符是指3D數組的單元格,我需要將這些字符串轉換為整數以獲取單元格地址,我使用以下命令:

string str = clickedButton.Name.ToString();
int xi ;
int xj;
int xz;
xi = Convert.ToInt32(str[1]);
xj = Convert.ToInt32(str[2]);
xz = Convert.ToInt32(str[3]);

請注意,我不使用“ str [0]”,因為它是“ p”。

但是當我編譯代碼時,xi,xj,xz的值是字符串字符的Ascci值。

我應該如何將字符串轉換為int以便不會發生?

請執行下列操作:

int xi = (int)Char.GetNumericValue(str[1]);

也參考這個

您的代碼在該位置的字符之后。

您想要的是將字符串放在該位置。

xi = Convert.ToInt32(str.Substring(1,1));
xj = Convert.ToInt32(str.Substring(2,1));
xk = Convert.ToInt32(str.Substring(3,1));

為什么不格式化字符串以直接從名稱中刪除p?

string str = clickedButton.Name.ToString();
str = str.Replace("p","");
int a = Convert.ToInt32(str);
int onesplace = a%10;
int secondplace = (a/10)%10;
int thirdplace = a/100;

“位置”從右到左開始。 一個地方是最正確的數字

如果需要單元格的各個值,則可以將此字符串轉換為整數並使用模數函數

編輯:更新的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM