0

im trying to replace normal text with ascii text in this program:

so a will be replaced by â & ETC.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextConverter
{
    public partial class TextCoverter : Form
    {
        public TextCoverter()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] normal = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
            string[] ascii = { "â", "ß", "ç", "ð", "è", "ƒ", "ģ", "н", "ι", "j", "ќ", "ļ", "м", "и", "ю", "ρ", "Ω", "ѓ", "$", "τ", "ט", "Λ", "ш", "χ", "У", "ź" };


            for (int i = 0;  i < 26; i++)
            {
                textBox2.Text = textBox1.Text.Replace(normal[i], ascii[i]);
            }

        }

    }
}

But it doesn't replace with Ascii. Please help.

3
  • 1
    Not a solution for your problem, but actually a-z are ASCII, the others are extended ASCII... see here Commented Jul 15, 2012 at 18:01
  • 1
    Every time you use the original text as source, so textbox2 will only have zs replaced. Commented Jul 15, 2012 at 18:01
  • I suggest you read about ASCII. Unless you meant extended ASCII. ;) Commented Jul 15, 2012 at 18:02

2 Answers 2

3

Since you are writing the result into a variable that is different from the original, only the last letter gets replaced. You should either write to the same box, or write to a temp string, and write it to the second box at the end.

var tmp = textBox1.Text;
for (int i = 0;  i < 26; i++)
{
    tmp = tmp.Replace(normal[i], ascii[i]);
}
textBox2.Text = tmp;

Generally speaking, this is not the most efficient algorithm to do replacements, because it operates on an immutable string. You would be better off creating a mutable string builder, and writing it one character at a time.

const string repl = "âßçðèƒģнιjќļмиюρΩѓ$τטΛшχУź";
var res = new StringBuilder();
foreach (char c in textBox1.Text) {
    if (c >= 'a' && c <= 'z') {
        res.Append(repl[c-'a']);
    } else {
        res.Append(c);
    }
}
textBox2.Text = res.ToString();
Sign up to request clarification or add additional context in comments.

4 Comments

const string repl = "âßçðèƒģнιjќļмиюρΩѓ$τטΛшχУź"; var res = new StringBuilder(); foreach (char c in textBox1.Text) { if (c >= 'a' && c <= 'z') { res.Append(repl[c - 'a']); } else { res.Append(c); } } textBox2.Text = res.ToString(); // it didnt work..
@SephIrockthis It should totally work. What didn't work for you?
@SephIrockthis Did it compile? Did you try stepping through the code in the debugger? Did the first method work?
@dasblinkenlight :: i used the first code it worked thank you very much!
0

textBox2.Text = textBox1.Text.Replace(normal[i], ascii[i]); you replace textBox1 again and again, but not save previos state, so work only last loop iteration

1 Comment

That's good for identifying the problem, but you can also suggest how to achieve what he is trying to do.

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.