首页 > 解决方案 > 通过字符串的某个位置获取ascii的char

问题描述

我想通过 c# 上字符串的某个位置获取 ascii 的字符,但我无法以正确的方式到达。

代码:

string Str;
string StrEnd;
int Clave = 0;

Str = txtStr.Text;
Clave = Convert.ToInt32(txtCv.Text);

for (int i = 0; i == Str.Length - 1; i++)
{
    StrEnd = (char)Encoding.ASCII.GetBytes(Str[index: i]) + Clave;
}

对于EG。

Str = "ABC"var Clave = 1然后string StrEnd会是这个样子"BCD"

另一个例子是......

Str = "Hola",Clave = 2那么StrEnd = "Jqnc"

如果还不清楚,请告诉我。

提前致谢。

标签: c#

解决方案


它缺少 Linq 解决方案:(
如果使用的 C# 版本不允许使用 auto [out] 参数,请添加int key = 0;)。

string Str = "Hola";
if (int.TryParse(txtCv.Text, NumberStyles.Integer, CultureInfo.CurrentCulture, out int key))
{
    string StrEnd = string.Join("", Str.Select(c => (char)(c + key)));
}


key == 2StrEnd = "Jqnc"


推荐阅读