简体   繁体   中英

I am trying to search array for Unicode value

I have a large 2D jagged edge array

string[][] MagicString = new string[417][];
MagicString[0] = new string[] { "Winner", "\u0041", "\u0042", "\u0043"};
//and so on only show first item in 2D here.

The strings are \\u to make them Unicode value. I have them as Unicode as we move through the Array we can get Characters from outside the ASCII range. I also uses these values to display the Magic string in a different tab. In the case above we see A in a cell then B in next cell and then C etc. All this Unicode strings works fine.

string[] InnerCharArray = MagicString[cmbobx_sounds.SelectedIndex];
..
..
dataGridView1[grid_y, grid_x].Value = InnerCharArray[char_num];

Now the bit I cannot get to work. In a different tab the user inputs a character and we check through the arrays to find it then print out the first item which may say "Winner"

In the string below called Unicodevalue for input of AI cannot get it to be \A. It will always return double slash but I only want one slash \\ \A. How can I read the text from the RichTextBox and turn it into a string \A, same as I have in the Array above?

for (int i = 0; i < rchtxbx_input.Text.Length; i++)
        {                
            string UnicodeValue =  @"\u" + String.Format("{0:x4}", Convert.ToInt32(rchtxbx_input.Text[i]));
 //tried "\\u" @"\u" just u removed u altogether

            for (int j = 0; j < MagicString.Length; j++)
            {
                string[] InnerCharArray = MagicString[j];
                bool has = InnerCharArray.Contains(UnicodeValue);
/never goes true even when I type in A which is 0041 as in Array

                if (has) rchtxbx_output.AppendText(InnerCharArray[0]);
            }
        }

The thing is, that you do not store "\A" in your string array MagicString . You put "\A" in there, which is a correct unicode syntax, so c# converts it to "A" (or whatever character 41 is) internally.

Your mistake is in the declaration of the MagicString . There you need to put @"\A" . If that doesn't work, cheat it by putting @"\\" + "u0041" to have C# store it as string.

Thanks for help. Grisgram gave me the clue I had missed. In the array "\A" is a character and not a string. Once I see that I worked out how do sort it out. I have to leave them as Unicode Char as I draw them to the gridview.

So after a few changes I now get the Unicode chars into ints and then use them

int InPutChar = Convert.ToInt32(rchtxbx_input.Text[i]);

char c = '\u3400';
int LowestMagicChar = c;

The rest of the code snippet is below

for (int i = 0; i < rchtxbx_input.Text.Length; i++)
        {

            string answers = "";

            int InPutChar = Convert.ToInt32(rchtxbx_input.Text[i]);

            char c = '\u3400';
            // Implicit conversion: char is basically a 16-bit unsigned integer
            int LowestMagicChar = c;

            bool doCheck = true, multipleChar = false; //set true if statements will set false.

            if (InPutChar < LowestMagicChar) 
            {
                rchtxbx_output.AppendText(rchtxbx_input.Text[i].ToString());
                doCheck = false;
            }

            if (doCheck == true) //Could be MagicChar so more checks inside
            {
                for (int j = 0; j < MagicString.Length; j++)
                {

                    string[] InnerCharArray = MagicString[j];

                    for (int k = 1; k < InnerCharArray.Length; k++)
                    {
                        string unichar = InnerCharArray[k]; //Get next char to check


                        if (rchtxbx_input.Text[i].ToString() == unichar) //&& (doCheck))
                                                                         //check with the char in textbox
                        {
                            if (doCheck == true)
                            {
                                answers = InnerCharArray[0]; //first time in we have one char
                            }
                            else
                            {
                                answers += ";" + InnerCharArray[0]; //comes in here if multiple chars for same sound
                                multipleChar = true;
                            }
                            // rchtxbx_output.AppendText(InnerCharArray[0] + " ");
                            //If the char then put down the sound and space.
                            doCheck = false;

Now I have it working I just need to make it better and quicker. Thanks to all for the help, often we do not see the obvious till it is pointed out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM