How to replace items in an array with corresponding array enumerator value string.
string a = "12345"; //Check if exists.
string[] b = a.Select(x => x.ToString()).ToArray();
string c = "54321"; // Replacement values.
string[] d = c.Select(y => y.ToString()).ToArray();
string e = "0A9B8C7D6E5F4G3H2I1"; // String to amend.
string[] f = e.Select(z => z.ToString()).ToArray();
foreach (string num in b)
{
replace = f.Select(x => x.Replace(b //Current b enumerator, d //enumerator = b current)).ToArray()
}
// Output Replace = "0A9B8C7D6E1F2G3H4I5";
Clarifying the question, you want to apply substitution cypher.
A substitution cipher simply replaces each letter or symbol with a different one. This is a classic method for encryption that was used by Julius Caesar. As a variation on this idea, you can encrypt the original letters to an entirely different set of symbols.
A short way to do so is using Linq. It's not very readable, but it does the job:
var a = "12345"; // original chars
var c = "54321"; // replacement chars
var e = "0A9B8C7D6E5F4G3H2I1"; // source string
var r = String.Concat(e.Select(x=>a.Contains(x)?c[a.IndexOf(x)]:x));
Resulting string r will be:
r == "0A9B8C7D6E1F2G3H4I5"
Remember to include at your header:
using System.Linq;
How about the following:
var a = "12345";
var c = "54321";
var e = "0A9B8C7D6E5F4G3H2I1";
// create a mutable copy of e
var builder = new StringBuilder(e);
for (var i = 0; i < e.Length; ++i) // for each character position in e
{
// look for that character in a
var index = a.IndexOf(e[i]);
// if we found it, replace the character at that position with the
// corresponding character from c
if (index >= 0) { builder[i] = c[index]; }
}
var result = builder.ToString(); // 0A9B8C7D6E1F2G3H4I5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.