hi.

i'm trying to convert a value from an array to a value from another array based on the index

ex:

string[] abc = {"a", "b", "c"};
string[] xyz = {"1", "2", "3"};


so let's say a have a text "aabbcc" .. and where the items from string abc are found are replaced with the items from string xyz based on index.. or something
output shoudl be "112233" . any ideas?
i'm trying somehow to connect the two string :|

Hmmm..if you mean like swapping the values of your letter array to your number array and vice versa...

put the values in to a new array..then swap them from that...i dont think there is a method in c# that actually does this...Maybe someone can clarify? (I would be interested to know if there is...

string[] letterarray = { "A", "B", "C" };

            string[] numberarray = { "1", "2", "3" };

            string[] newletterarray = new string[3];
                                                      //new arrays to store values
            string[] newnumberarray = new string[3];

            for (int i = 0; i < 3; i++)
            {
               newletterarray[i] = letterarray[i];
               newnumberarray[i] = numberarray[i];
            }
            for (int i = 0; i < 3; i++)
            {                                         //swap values using for loops
                numberarray[i] = newletterarray[i];
                letterarray[i] = newnumberarray[i];
            }

Try something like this:

void button1_Click(object sender, EventArgs e)
    {
      string input = "xaabbccx";
      string[] arr1 = new string[] { "a", "b", "c" };
      string[] arr2 = new string[] { "1", "2", "3" };

      Dictionary<string, string> dict = new Dictionary<string, string>(arr1.Length, StringComparer.OrdinalIgnoreCase);
      for (int i1 = 0; i1 < arr1.Length; i1++)
      {
        dict.Add(arr1[i1], arr2[i1]);
      }

      StringBuilder sb = new StringBuilder(input.Length);
      for (int i1 = 0; i1 < input.Length; i1++)
      {
        string s;
        if (!dict.TryGetValue(input[i1].ToString(), out s))
          s = input[i1].ToString();
        sb.Append(s);
      }

      Console.WriteLine(sb.ToString());
    }

Results:

x112233x
commented: Who is this fellow? ha.ha..! Scott :) Welcome back! :) +11

Try something like this:

void button1_Click(object sender, EventArgs e)
    {
      string input = "xaabbccx";
      string[] arr1 = new string[] { "a", "b", "c" };
      string[] arr2 = new string[] { "1", "2", "3" };

      Dictionary<string, string> dict = new Dictionary<string, string>(arr1.Length, StringComparer.OrdinalIgnoreCase);
      for (int i1 = 0; i1 < arr1.Length; i1++)
      {
        dict.Add(arr1[i1], arr2[i1]);
      }

      StringBuilder sb = new StringBuilder(input.Length);
      for (int i1 = 0; i1 < input.Length; i1++)
      {
        string s;
        if (!dict.TryGetValue(input[i1].ToString(), out s))
          s = input[i1].ToString();
        sb.Append(s);
      }

      Console.WriteLine(sb.ToString());
    }

Results:

x112233x

i've tried this code .. works
but when i add a multiple letter element in the string (ex: "aaa") doesn't do anything. what am i doing wrong?

Do use Dictionary to store roots.

Dictionary<string, string> elements = new Dictionary<string, string>()
            {
               {"a","1"},
               {"b","2"},
               {"c","3"}
            };

            string text = "aabb2wccaaabbber";

            var result =string.Join("",(from n in text
                          select elements.ContainsKey(n.ToString()) ? elements[n.ToString()] : n.ToString()).ToArray());

            Console.WriteLine(result);

Do use Dictionary to store roots.

Dictionary<string, string> elements = new Dictionary<string, string>()
            {
               {"a","1"},
               {"b","2"},
               {"c","3"}
            };

            string text = "aabb2wccaaabbber";

            var result =string.Join("",(from n in text
                          select elements.ContainsKey(n.ToString()) ? elements[n.ToString()] : n.ToString()).ToArray());

            Console.WriteLine(result);

if i do this :

Dictionary<string, string> elements = new Dictionary<string, string>()
            {
               {"aa","1"},
               {"bb","2"},
               {"c","3"}
            };

            string text = "aabb2wccaaabbber";

            var result =string.Join("",(from n in text
                          select elements.ContainsKey(n.ToString()) ? elements[n.ToString()] : n.ToString()).ToArray());

            Console.WriteLine(result);

doesnt work anymore

private void button1_Click(object sender, EventArgs e){
            
            string txt1 = textBox1.Text;
            
            string[] split = txt1.Split(new Char[] { ' ', ',', '\n' });
            Dictionary<string, string> elements = new Dictionary<string, string>()
            {
               {"ad","0"},
               {"sb","1"},
               {"ml", "2"},
               {"dv", "3"},
               {"adi", "4"},
               {"sdi", "5"},
               {"rti", "6"},
               {"sh",  "7"},
               {"and", "8"},
               {"or", "9"},
               {"xor", "A"},
               {"msk", "B"},
               {"ld", "C"},
               {"st", "D"},
               {"bri", "E8"},
               {"brv", "E4"},
               {"brn", "E2"},
               {"brz", "E1"},
               {"bii", "F8"},
               {"biv", "F4"},
               {"bin", "F2"},
               {"biz", "F1"},
               {"r0", "0"}, {"r1", "1"}, {"r2", "2"}, {"r3", "3"}, {"r4", "4"}, {"r5", "5"}, {"r6", "6"}, {"r7", "7"}, {"r8", "8"}, {"r9", "9"}, {"r10", "A"}, {"r11", "B"}, {"r12", "C"}, {"r13", "D"}, {"r14", "E"}, {"r15", "F"}
            };         
            for (int i = 0; i < split.Length; i++)
            {
                if (elements.ContainsKey(split[i]))
                {
                    textBox2.Text = elements[split[i]];
                    
                }
            }

        }

ok. so this is the code

i have a multiline textbox with a text like this :
adi r1,r1,1
ad r2,r2,r1
sbi r3,r3,1

i split it. done that.
after this i try to compare every split with the keys in the dictionary and in textbox2 i must have the value for each split
something like this:

textbox 1        ->>    textbox2
adi r1,r1,1             4111
ad r2,r2,r1             0221
sbi r3,r3,1             5331

the code so far only replaces the textbox1.text only if it's one key
ex :

textbox 1        ->>    textbox2
adi                      4

how can i do this? or maybe there's a better method?
pls help :yawn:

public void check()
        {

            string txt1 = textBox1.Text;
            string[] split;
            //string some = " ";
            string key = elements.Keys.ToString();
            split = txt1.Split(new Char[] { ' ', ',', '\n' });
            textBox2.Lines = split;
            for( int i =0; i < split.Length;i++)
            if (elements.ContainsKey(split[i]))
                    {
                        
                        textBox2.Lines = elements.TryGetValue(split[i]);
                    }
                
            
          }

i've compared the key with the split .. seems to work
now i wanna get in textbox2 the value associated with the split . suggestions, pls :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.