2

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)

https://i.sstatic.net/SyK4M.png

2
  • 1
    You need to either use a more functional replace algorithm (e.g. regular expression replace) or chain the calls to replace. The problem with chaining the calls may be that you overwrite previous replaces if you don't chain in an order that prevents this - for instance - textVar.Replace('A', 'z').Replace('z', 'C') would result in all your z characters being replaced by C in the final output Commented Jun 6, 2014 at 14:33
  • You should post your code in your question, not in a linked image. The reason it's not working the way you expect is that you do all the replacements using txt1 (which never changes), and only keep the result of the last one in txt2. Commented Jun 6, 2014 at 14:33

3 Answers 3

3

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the most helpful answer.
3

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();

4 Comments

This is broken, because subsequent substitutions see the results of previous ones.
@dasblinkenlight Then that's a problem in the OPs alright, right? The code I've shown does what the OP wants it to do.
Of course! I am pointing out that the problem is not only in the implementation of the algorithm, but in the algorithm itself.
@dasblinkenlight Agreed. @Nirex, as I've already explained, that's because you're calling Replace on the same string over and over again. Each time you call Replace, the result from the previous call will be forgotten.
0

You keep applying the replacement to your original value. Replace returns a new String instead of modifying the one you pass in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.