C# 把ABCD转换成数字
每倒题得选项可能是多选或者单选。
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(); }