C# 字符串中单个字符的反向字母值
我想知道如何反转字符串中的单个字符,而不反转整个字符串。我使用了下面显示的代码,得到了以下结果。我还将展示结果应该是什么C# 字符串中单个字符的反向字母值,c#,C#,我想知道如何反转字符串中的单个字符,而不反转整个字符串。我使用了下面显示的代码,得到了以下结果。我还将展示结果应该是什么 输入:a29z3 输出:a20a3 应该是什么:z70a6 正如你所看到的,我并没有得到我一开始所期望的结果。每个字符都应该反转,但我很好奇怎么做。上面的结果是我通过以下代码得到的: static void Main() { string input = "a29z3"; //Reverse Letters inp
- 输入:a29z3
- 输出:a20a3
- 应该是什么:z70a6
static void Main()
{
string input = "a29z3";
//Reverse Letters
input = input.Replace("a", "z");
input = input.Replace("b", "y");
input = input.Replace("c", "x");
input = input.Replace("d", "w");
input = input.Replace("e", "v");
input = input.Replace("f", "u");
input = input.Replace("g", "t");
input = input.Replace("h", "s");
input = input.Replace("i", "r");
input = input.Replace("j", "q");
input = input.Replace("k", "p");
input = input.Replace("l", "o");
input = input.Replace("m", "n");
input = input.Replace("n", "m");
input = input.Replace("o", "l");
input = input.Replace("p", "k");
input = input.Replace("q", "j");
input = input.Replace("r", "i");
input = input.Replace("s", "h");
input = input.Replace("t", "g");
input = input.Replace("u", "f");
input = input.Replace("v", "e");
input = input.Replace("w", "d");
input = input.Replace("x", "c");
input = input.Replace("y", "b");
input = input.Replace("z", "a");
//Reverse numbers
input = input.Replace("0", "9");
input = input.Replace("1", "8");
input = input.Replace("2", "7");
input = input.Replace("3", "6");
input = input.Replace("4", "5");
input = input.Replace("5", "4");
input = input.Replace("6", "3");
input = input.Replace("7", "2");
input = input.Replace("8", "1");
input = input.Replace("9", "0");
Console.WriteLine(input);
}
建议和/或示例将非常受欢迎 问题是您要替换相同的字母两次。您需要在字符串中一次循环一个项目,并替换每个字符,而不是在整个字符串上重复运行replace 例如,类似这样的内容:
static void Main(string[] args) {
string input = "a29z3";
string output = string.Empty;
foreach (char letter in input.ToCharArray()) {
output += replacementChar(letter);
}
Console.WriteLine(output);
}
static char replacementChar(char c) {
switch (c) {
case 'a': return 'z';
case 'b': return 'y';
// and so on
default: return c;
}
}
我还建议使用对ASCII值的计算来进行交换,而不是单独列出一长串替换。将更干净。您遇到的问题是您正在对已替换的角色进行替换。将
a
替换为z
,然后将z
替换为a
我会使用字典查找值并循环字符,生成一个新字符串:
Dictionary<string, string> ReplaceDict = new Dictionary<string, string>();
private void Form1_Load(object sender, EventArgs e)
{
//Reverse Letters
ReplaceDict.Add("a", "z");
ReplaceDict.Add("b", "y");
ReplaceDict.Add("c", "x");
ReplaceDict.Add("d", "w");
ReplaceDict.Add("e", "v");
ReplaceDict.Add("f", "u");
ReplaceDict.Add("g", "t");
ReplaceDict.Add("h", "s");
ReplaceDict.Add("i", "r");
ReplaceDict.Add("j", "q");
ReplaceDict.Add("k", "p");
ReplaceDict.Add("l", "o");
ReplaceDict.Add("m", "n");
ReplaceDict.Add("n", "m");
ReplaceDict.Add("o", "l");
ReplaceDict.Add("p", "k");
ReplaceDict.Add("q", "j");
ReplaceDict.Add("r", "i");
ReplaceDict.Add("s", "h");
ReplaceDict.Add("t", "g");
ReplaceDict.Add("u", "f");
ReplaceDict.Add("v", "e");
ReplaceDict.Add("w", "d");
ReplaceDict.Add("x", "c");
ReplaceDict.Add("y", "b");
ReplaceDict.Add("z", "a");
//Reverse numbers
ReplaceDict.Add("0", "9");
ReplaceDict.Add("1", "8");
ReplaceDict.Add("2", "7");
ReplaceDict.Add("3", "6");
ReplaceDict.Add("4", "5");
ReplaceDict.Add("5", "4");
ReplaceDict.Add("6", "3");
ReplaceDict.Add("7", "2");
ReplaceDict.Add("8", "1");
ReplaceDict.Add("9", "0");
}
private void button1_Click_1(object sender, EventArgs e)
{
StringBuilder output = new StringBuilder();
string input = "a29z3";
foreach (var c in input.ToCharArray())
{
output.Append(ReplaceDict[c.ToString()]);
}
MessageBox.Show(output.ToString());
}
Dictionary ReplaceDict=new Dictionary();
私有void Form1\u加载(对象发送方、事件参数e)
{
//反向字母
替换。添加(“a”、“z”);
替换。添加(“b”、“y”);
替换条款添加(“c”、“x”);
替换条款添加(“d”、“w”);
替换。添加(“e”、“v”);
替换。添加(“f”、“u”);
替换。添加(“g”、“t”);
替换。添加(“h”、“s”);
替换。添加(“i”、“r”);
添加(“j”、“q”);
添加(“k”、“p”);
替换。添加(“l”、“o”);
替换条款添加(“m”、“n”);
替换条款添加(“n”、“m”);
替换。添加(“o”、“l”);
添加(“p”、“k”);
添加(“q”、“j”);
替换条款添加(“r”、“i”);
替换条款添加(“s”、“h”);
替换。添加(“t”、“g”);
替换。添加(“u”、“f”);
替换条款添加(“v”、“e”);
替换条款添加(“w”、“d”);
替换条款添加(“x”、“c”);
替换。添加(“y”、“b”);
替换条款。添加(“z”、“a”);
//反向编号
添加(“0”、“9”);
替换条款添加(“1”、“8”);
替换条款添加(“2”、“7”);
替换条款添加(“3”、“6”);
替换条款添加(“4”、“5”);
替换条款添加(“5”、“4”);
替换条款添加(“6”、“3”);
替换条款添加(“7”、“2”);
替换条款添加(“8”、“1”);
替换条款添加(“9”、“0”);
}
私有无效按钮1\u单击\u 1(对象发送者,事件参数e)
{
StringBuilder输出=新的StringBuilder();
字符串输入=“a29z3”;
foreach(input.ToCharArray()中的var c)
{
Append(ReplaceDict[c.ToString()]);
}
Show(output.ToString());
}
发生的事情是:
input=“a”
input=input.Replace(“a”、“z”)代码>执行,现在input=“z”
input=input.Replace(“A”、“z”)代码>被执行,现在input=“a”
您需要做的不是替换,而是构建一个新字符串。因此,您遍历字符串中的字符,找到相反的字符,并将其附加到新字符串中 这假设您的字符都在预期范围内,但您可以执行以下操作:
public static char Reverse(char c)
{
if (Char.IsDigit(c))
{
return (char)((int)'9' - (int)c + (int)'0');
}
else
{
return (char)((int)'z' - (int)c + (int)'a');
}
}
然后使用选择进行映射:
string output = new string(input.Select(Reverse).ToArray());
正如您的评论者所发布的,您正在更改已经更改的角色
在给定的示例中,这将是代码的流程:
a29z3
z29z3 -> a to z
a29a3 -> z to a
a79a3 -> 2 to 7
a79a6 -> 3 to 6
a79a3 -> 6 to 3
a29a3 -> 7 to 2
a20a3 -> 9 to 0
一个班轮(代码高尔夫):
input=input.Aggregate(string.Empty,(s,c)=>s+((c>='a')&&&(c='0')&&&(c其他人已经发现了这个问题:您将一个字符替换为另一个字符,但在某些情况下再次将该字符替换为其原始值
为了更好地编写代码,让我们重申这个问题:您有两个连续字符范围(a-z和0-9),并且希望在每个范围内反转字符
因此,对于单个字符,我们测试它是否在每个范围内,如果在每个范围内,我们将其反转:
private static readonly Tuple<char, char>[] Ranges =
new[] { Tuple.Create('a', 'z'), Tuple.Create('0', '9') };
static char Reverse(char c)
{
foreach (var range in Ranges)
{
var first = range.Item1;
var last = range.Item2;
if (c >= first && c <= last)
{
int index = c - first;
int reverseIndex = (last - first) - index;
return (char)(first + reverseIndex);
}
}
return c;
}
ToCharArray
的使用可以直接删除。string
已经实现了IEnumerable
。而且Concat
比Union
更有意义。当你知道没有重复项时,主动检查重复项是没有意义的。@Servy:谢谢你的评论!
input = input.Aggregate(string.Empty, (s, c) => s + ((c >= 'a') && (c <= 'z') ? (char)('z' - c + 'a') : (c >= '0') && (c <= '9') ? (char)('9' - c + '0') : c));
private static readonly Tuple<char, char>[] Ranges =
new[] { Tuple.Create('a', 'z'), Tuple.Create('0', '9') };
static char Reverse(char c)
{
foreach (var range in Ranges)
{
var first = range.Item1;
var last = range.Item2;
if (c >= first && c <= last)
{
int index = c - first;
int reverseIndex = (last - first) - index;
return (char)(first + reverseIndex);
}
}
return c;
}
static string Reverse(string s)
{
return string.Concat(s.Select(Reverse));
}