0

I have two string arrays which are:

string[] abecele = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "w", "x", "y", "z", ".", ",", "!", "?", "(", ")", "@", " " };
string[] keitiniai = { "714", "741", "147", "471", "417", "174", "789", "798", "897", "978", "879", "987", "123", "321", "132", "312", "213", "231", "852", "258", "825", "285", "582", "528", "951", "159", "915", "195", "519", "591", "753", "357", "000" };

Then I have a string named tekstas, which takes some rondom text from maskedTextBox: tekstas = maskedTextBox1.Text;

Now I need that characters which are in abecele[] array IN THAT TEXT would be changed to keitiniai arrays values, like if we had such text "abc" in tekstas string it would become 714741147.

Im using such code to implement that:

for (i = 0; i < 32; i++)
{
    string raide = abecele[i];
    string keitinys = keitiniai[i];
    string pakeistas = tekstas.Replace(raide, keitinys);
}

But new string pakeistas which should be replaced as I want is not replaced. Where is the problem?

1
  • 2
    If you try to encrypt a password, use SHA-1 hashing algorithm (if you only need to compare it) or AES encryption (if you need to decrypt the password). It's safer. Commented May 30, 2012 at 8:50

4 Answers 4

4

At the moment you're discarding the "replacements made" string each time. You probably want the following:

string pakeistas = tekstas;
for (i = 0; i < 32; i++)
{
    string raide = abecele[i];
    string keitinys = keitiniai[i];
    pakeistas = pakeistas.Replace(raide, keitinys);
}

// Use pakeistas for whatever.
Sign up to request clarification or add additional context in comments.

Comments

4

I would use a Dictionary<Char, String> instead:

IDictionary<char, string> lookup = new Dictionary<char, string> {
    {'a', "714"},
    {'b', "741"},
    {'c', "147"},
    //...
};
StringBuilder resultBuilder = new StringBuilder();
foreach(char c in tekstas) {
    string code;
    if(lookup.TryGetValue(c, out code))
        resultBuilder.Append(code);
}
string result = resultBuilder.ToString();

Comments

2

Your new modified string, pakeistas, gets overwritten over and over, and exists only during the for loop.

Comments

1

instead of having those two arrays, you can have Dictionary with key an alphabet and value as your integer values and then you can use StringBuilder to create your string from abc to 714741147

Dictionary<string, string> myDictionary = new Dictionary<string, string>();
myDictionary.Add("a", "714");
myDictionary.Add("b", "741");
myDictionary.Add("c", "147");
//and so on ...
string tekstas = "abc";
StringBuilder sb = new StringBuilder();
foreach(char c in tekstas)
       {
         string key = c.ToString();
         sb.Append(myDictionary[key]);
         }
var  pakeistas = sb.ToString();

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.