c 列舉轉化示例大全,數字或字串轉列舉

2021-09-08 02:24:11 字數 1157 閱讀 2243

列舉轉化示例大全,數字或字串轉列舉,本文重點舉例說明c#列舉的用法,數字轉化為列舉、列舉轉化為數字及其列舉數值的判斷,以下是具體的示例:

字串轉換成列舉:dayofweek week= (dayofweek)enum.parse(typeof(dayofweek), "friday"); 數字轉換成列舉:dayofweek week= (dayofweek)5;  //friday 具體的示例: 定義列舉: public enum displaytype

1.數值轉化 (1)字元轉化為列舉 string str="up"; displaytype displaytype; displaytype=(displaytype)system.enum.parse(typeof(displaytype),str,true); response.write(displaytype.tostring());

結果是:up enum.parse 方法第3個引數,如果為 true,則忽略大小寫;否則考慮大小寫。

(2)數字轉化為列舉 int i=30; displaytype displaytype; displaytype=(displaytype)system.enum.parse(typeof(displaytype),i.tostring()); response.write(displaytype.tostring()); 結果是:down (3)列舉轉化為字元 displaytype displaytype=displaytype.down; string str=displaytype.tostring(); response.write(str); 結果是:down

(4)列舉轉化為數字 方法一: displaytype displaytype=displaytype.down; int i=convert.toint32(displaytype.tostring("d")); response.write(i.tostring());

或者:(int)enum.parse(typrof(displaytype),"down") 結果是:30

方法二: displaytype displaytype=displaytype.down; int i=((iconvertible)((system.enum)displaytype)).toint32(null); response.write(i.tostring()); 結果是:30

字串轉化數字演算法(C )

自定義數字解析函式 說明 如果輸入的字串為空或非數字開頭則返回0,否則返回解析結果 輸入的字串 解析後的結果 static double coustomnumber string str if string.isnullorempty str return 0d 如果輸入的字串為空或null,則直接...

c 字串轉數字或數字轉字串

在c 中字串轉換為數字,或數字轉換為字串,用到如下函式 itoa atoi atof itoa itow itoa s 1.整形轉換為字串 2.字串轉為整形 在字符集設定不同下會有不同的型別,說白了,這幾個函式的功能都相同,但是根據你的字符集不同,選用的函式也不同。itot 在asicii下被巨集定...

字串轉化為數字

如何把entry中輸入的字串轉化為對應的數字 例如,如果輸入為s 0 12 3 40 怎麼把它轉化為對應的數字呢?根據需要,胡亂寫了 記錄在此。功能 把字串轉化為數字列表 輸入s 字串 輸出 數字列表 def strtonum s s 0 12 3 40 p 用來存放字串中的數字 0,1,2,3,4...