C# 把ABCD轉換成數字
阿新 • • 發佈:2018-06-20
replace nsf man light static sel ati ray ace
每倒題得選項可能是多選或者單選。
public static string LetterTransformationNum(string answer, int type) { string num = ""; if (type == 1 || type == 2) //(1和2代表單選和多選) { var str = answer; //去答案 str = TrimAllHtmlTag(str).Replace("】", ""); StringBuilder sp = new StringBuilder(); for (int i = 0; i < str.Length; i++) { if (str[i] >= ‘A‘ && str[i] <= ‘G‘) { sp.Append((char)str[i] - ‘A‘); } else { sp.Append(str[i]); } } object numanswer = sp.ToString(); num = Convert.ToString(numanswer); } num = String.Join(",", (from c in num select c + "").ToArray()); //加, string answerValue = "[" + num + "]"; return answerValue; }
去掉一些特殊得字符正則
public static string TrimAllHtmlTag(string str) { string reg = "<([^>]+?)>"; return Regex.Replace(str, reg, "").Trim(); }
C# 把ABCD轉換成數字