将文本转换为带有坐标的数字
本文关键字:坐标 数字 文本 转换 | 更新日期: 2023-09-27 17:51:16
今天我想做一些有趣的东西,并决定做一个简单的程序,将和平的文本转换成数字。我试过这个代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AlphaNumCrypt
{
public partial class Form1 : Form
{
public String input;
public String output;
public Form1()
{
InitializeComponent();
}
private void translateButton_Click(object sender, EventArgs e)
{
// Encrypt/Decrypt input
if (toText.Checked == true)
{
// Decrypt input
input = "";
input = inputTextBox.Text.ToString();
decrypt();
}
else if (toAlphaNum.Checked == true)
{
// Encrypt input
input = "";
input = inputTextBox.Text.ToString();
encrypt();
}
}
private void encrypt()
{
// Encrypt the plain text input
output = input;
output.Replace("a", "11");
output.Replace("b", "12");
output.Replace("c", "13");
output.Replace("d", "14");
output.Replace("e", "15");
output.Replace("f", "16");
output.Replace("g", "17");
output.Replace("h", "18");
output.Replace("i", "19");
output.Replace("j", "21");
output.Replace("k", "22");
output.Replace("l", "23");
output.Replace("m", "24");
output.Replace("n", "25");
output.Replace("o", "26");
output.Replace("p", "27");
output.Replace("q", "28");
output.Replace("r", "29");
output.Replace("s", "31");
output.Replace("t", "32");
output.Replace("u", "33");
output.Replace("v", "34");
output.Replace("w", "35");
output.Replace("x", "36");
output.Replace("y", "37");
output.Replace("z", "38");
output.Replace("æ", "39");
output.Replace("ø", "41");
output.Replace("å", "42");
outputTextBox.Text = output;
}
private void decrypt()
{
// Decrypt the AlphaNum input
output = input;
output.Replace("11", "a");
output.Replace("12", "b");
output.Replace("13", "c");
output.Replace("14", "d");
output.Replace("15", "e");
output.Replace("16", "f");
output.Replace("17", "g");
output.Replace("18", "h");
output.Replace("19", "i");
output.Replace("21", "j");
output.Replace("22", "k");
output.Replace("23", "l");
output.Replace("24", "m");
output.Replace("25", "n");
output.Replace("26", "o");
output.Replace("27", "p");
output.Replace("28", "q");
output.Replace("29", "r");
output.Replace("31", "s");
output.Replace("32", "t");
output.Replace("33", "u");
output.Replace("34", "v");
output.Replace("35", "w");
output.Replace("36", "x");
output.Replace("37", "y");
output.Replace("38", "z");
output.Replace("39", "æ");
output.Replace("41", "ø");
output.Replace("42", "å");
outputTextBox.Text = output;
}
}
}
但是由于某种原因,输出等于输入,我在这里做错了什么?
问候,Erik
这是因为您没有使用output.Replace
方法的结果。它返回一个新字符串,不修改现有字符串。你应该这样修改你的代码:
output = output.Replace("a", "11");
...
。为了使代码更方便,您可以将转换规则存储为Tuple<string, string>
数组。因此,可以使用循环对文本进行编码和解码。下面是一个例子:
private readonly Tuple<string, string>[] rules = new[]
{
new Tuple<string, string>("a", "11"),
new Tuple<string, string>("b", "12"),
new Tuple<string, string>("c", "13"),
...
};
private string Encrypt(string input)
{
return rules.Aggregate(input, (current, rule) => current.Replace(rule.Item1, rule.Item2));
}
private string Decrypt(string input)
{
return rules.Aggregate(input, (current, rule) => current.Replace(rule.Item2, rule.Item1));
}
您必须存储由output.Replace
返回的结果,如:
output = input;
output = output.Replace("11", "a");
output = output.Replace("12", "b");
...
outputTextBox.Text = output;
.Replace()
方法是非破坏性的。它不会修改原始对象。它只返回一个基于应用函数的原始值的新值。所以你所要做的就是把对象重新赋值给返回的值:
output = output.Replace("o", "26");
您还可以考虑创建两个字典来处理映射,并且只循环遍历每个字母,而不是调用每个字母,无论它是否出现在原始字符串中。
为了完整:
在你的代码中使用StringBuilder
代替string
作为输出和输入。这样您就不必重新分配所有的替换行。您的代码应该按原样工作。只需从encrypt()
和decrypt()
函数中删除output = input
和input = output
行
Although StringBuilder and String both represent sequences of characters, they are implemented differently. String is an immutable type. That is, each operation that appears to modify a String object actually creates a new string.
For routines that perform extensive string manipulation (such as apps that modify a string numerous times in a loop), modifying a string repeatedly can exact a significant performance penalty. The alternative is to use StringBuilder, which is a mutable string class. Mutability means that once an instance of the class has been created, it can be modified by appending, removing, replacing, or inserting characters.