The issue here is that , application only takes the last Replace method (Z to G)
is there any way possible to make it Get all of them ? ((n to o) for example)
(here is the image)
The issue here is that , application only takes the last Replace method (Z to G)
is there any way possible to make it Get all of them ? ((n to o) for example)
(here is the image)
application only takes the last Replace method (Z to G)
This is because strings are immutable. More importantly, your algorithm is broken, because subsequent substitutions "see" the results of the previous ones. You cannot do the replacements sequentially - if you want to get correct results, you must consider them all at once. Otherwise, l will become o, even though your algorithm replaces l with n:
// Let's say the text is "Hello"
text = text.replace('l', 'n'); // The text becomes "Henno"
... // More substitutions
text = text.replace('n', 'o'); // The text becomes "Heooo"
Here is how you can fix it:
StringBuilder res = new StringBuilder();
foreach (char c in txt1.Text) {
char toAppend;
switch (c) {
case 'I': toAppend = 'L'; break;
case 'j': toAppend = 'n'; break;
case 'J': toAppend = 'N'; break;
case 'k': toAppend = 'l'; break;
...
default: toAppend = '?';
}
res.append(toAppend);
}
You're modifying the same source over and over again, instead of modifying the result from the previous call.
Try
string replaced = txt1.Text.Replace(...);
replaced = replaced.Replace(...);
replaced = replaced.Replace(...);
...
txt2.Text = replaced;
Or better yet, use StringBuilder to repeatedly mutate a string without littering the heap with intermediate strings.
StringBuilder sb = new StringBuilder(txt1.Text);
sb.Replace(...);
sb.Replace(...);
sb.Replace(...);
txt2.Text = sb.ToString();
Replace on the same string over and over again. Each time you call Replace, the result from the previous call will be forgotten.
textVar.Replace('A', 'z').Replace('z', 'C')would result in all yourzcharacters being replaced byCin the final outputtxt1(which never changes), and only keep the result of the last one intxt2.