Расшифруйте число строк в алфавите на C#
Dear All, I want to convert number string entered in a textbox into integer array, and later on getting alphabet according index value of character array, and value of integer array. Although, by using lambda expression, I can easily achieve that. But I want to achieve the same without lambda expression, and alternative regular expression for the same. Note:- In given below code, without lambda expression, if I enter “0AB12”, then it will print AABBC, instead of I want to ignore alphabet, and just want to decode in integer. While with lambda expression, it gives me perfect answer- ABC Please help, it was asked me in a recent interview as a fresher. protected void btn_Decode_Click(object sender, EventArgs e) { char[] arr = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' }; string result = string.Empty; //string input = '0231'; var myString = txt_Number.Text; int myInt; int[] num_arr = new int[myString.Length]; for (int i = 0; i <myString.Length; i++) { if (int.TryParse(myString.Substring(i,1),out myInt)) { num_arr[i] = int.Parse(myString.Substring(i, 1)); } } //int myInt; //var num_arr = myString.ToCharArray().Where(x => int.TryParse(x.ToString(), out myInt)).Select(x => int.Parse(x.ToString())).ToArray(); //int[] num_arr = new int[] { 0, 2, 3, 1 }; for (int i = 0; i < num_arr.Length; i++) { result = result + arr[num_arr[i]]; } Response.Write(result); }
Что я уже пробовал:
protected void btn_Decode_Click(object sender, EventArgs e) { char[] arr = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' }; string result = string.Empty; //string input = '0231'; var myString = txt_Number.Text; int myInt; int[] num_arr = new int[myString.Length]; for (int i = 0; i <myString.Length; i++) { if (int.TryParse(myString.Substring(i,1),out myInt)) { num_arr[i] = int.Parse(myString.Substring(i, 1)); } } //int myInt; //var num_arr = myString.ToCharArray().Where(x => int.TryParse(x.ToString(), out myInt)).Select(x => int.Parse(x.ToString())).ToArray(); //int[] num_arr = new int[] { 0, 2, 3, 1 }; for (int i = 0; i < num_arr.Length; i++) { result = result + arr[num_arr[i]]; } Response.Write(result); }