本文介绍了使用C#将整数字符串解码为字母表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有,
我想将文本框中输入的数字字符串转换为整数数组,然后根据字符数组的索引值和整数数组的值获取字母。
虽然通过使用lambda表达式,我可以轻松实现这一点。但是我希望在没有lambda表达式的情况下实现相同的目标,并为其提供替代正则表达式。
注意: - 在下面给出的代码中,没有lambda表达式,如果我输入0AB12,那么它将打印AABBC,而不是我想忽略字母表,只想整数解码。使用lambda表达式时,它给了我完美的答案 - ABC
请帮助,它在最近的采访中被问到我是一个新鲜的。
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()。其中(x => int.TryParse(x.ToString(),out myInt))。选择(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()。其中(x => int.TryParse(x.ToString(),out myInt))。选择(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);
}
解决方案
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); }
What I have tried:
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); }
解决方案
这篇关于使用C#将整数字符串解码为字母表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!