Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
7.0k views
in Technique[技术] by (71.8m points)

c# - checking each char in string is outputted in wrong order?

im trying to split a string into a chars(which i have semi done) and use each char to compare it to my dictionary full of random strings, and then add that chars value in the dictionary to a finished string. the problem is what its outputting- the chars are not in order.

ex: USER INPUT: "hello"---

ex: CONSOLE: "e"-"l"-"l"-"o"-"h"

thats what happens mostly. any answer as to how i can get it to spell it correctly in the console?

Code:

private void complete_text_Click(object sender, RoutedEventArgs e)
{
    end_result = input_box.Text.ToString();
    string end_translated_result = "";
    for (int i = 0; i < ZENOX_LANGUAGE.Keys.Count; i ++)
    {

        foreach (char iq in end_result.ToCharArray())
        {
            
            
            if (iq.ToString().ToLower() == ZENOX_LANGUAGE.Keys.ElementAt(i).ToString().ToLower())
            {
                Console.WriteLine(iq);
                end_translated_result += ZENOX_LANGUAGE.Values.ElementAt(i) + " ";
                
                //Console.WriteLine("sender: " + sender.ToString() + " c: " + end_result[iq].ToString().ToLower() + " s:" + ZENOX_LANGUAGE.Keys.ElementAt(i));
               Console.WriteLine("end translated result: " + end_result);
                
            }
            
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As it stands, the order of keys in your dictionary will influence the order that output appears, because you're doing:

foreach(var k in dictionary.Keys)
  foreach(char c in someString)
    if(c == k) 
      Console.Write(c)

And dictionary keys have no defined order.

Swapping the loops over will mean (as long as the dictionary has the key you're looking for, as it's a condition that leads to printing the char) that the output will appear in order of chars in the string..

..but I can't actually work out why you enumerate the keys and then run a loop looking for the character. I'd just loop over the string and use the char to index the dictionary if I was building some sort of translator map:

var map = new Dictionary<char, char>() {
  { 'h', 'Z' }, 
  { 'e', 'Y' }, 
  { 'l', 'X' }, 
  { 'o', 'W' } 
};

var toTrans = "hello";
foreach(char c in toTrans)
  Console.Write(map[c]);

This will print "ZYXXW" for an input of "hello";

If you're mapping chars to strings, with case insensitivity it's as simple as:

var map = new Dictionary<char, string>() {
  { 'h', "Z0" }, 
  { 'e', "Y0" }, 
  { 'l', "X0" }, 
  { 'o', "W0" } 
};

var toTrans = "HelLO";
foreach(char c in toTrans)
  Console.Write(map[Char.ToLower(c)]);

This will print "Z0Y0X0X0W0"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...