none
how to insert Korean letters into string variable? RRS feed

  • Question

  • hello

    I am trying to insert korean lettes("안녕하세요") into string.

    However, In debug time, the string becomes broken so I cannot read.

    please help me

    thank you.



    • Edited by leetoya Friday, February 23, 2018 8:12 AM
    Friday, February 23, 2018 4:39 AM

All replies

  • Hi,

    I'm not sure if I can understand your situation.
    How about, if you install Korean language pack into your Visual Studio?

    # Please provide version and edition of your Visual Studio.
       And International Setting of your VS.
       [Options] > [Tools] > [International Settings]
       

    Regards,

    Ashidacchi

    • Proposed as answer by Hart Wang Sunday, February 25, 2018 4:55 AM
    Friday, February 23, 2018 5:30 AM
  • Hi, Ashidacchi.

    thank you for your reply.

    I am using visual studio 2015 professional korean editon like below.

    I still does not know what the reason is. 


    • Edited by leetoya Wednesday, February 28, 2018 12:38 AM
    Wednesday, February 28, 2018 12:35 AM
  • Hi leetoya,

    Which Font are you using for the string? (e.g. Meiryo, Times New Roman, MS ゴシック)
    Does the font support Hangul letters?

    Regards,

    Ashidacchi

    Wednesday, February 28, 2018 1:02 AM
  • hi,

    Ashidacchi

    I am using consoals font.

    Even though i try differnt font style, but it does still not read korean in string.

    regards


    • Edited by leetoya Wednesday, February 28, 2018 1:38 AM
    Wednesday, February 28, 2018 1:37 AM
  • Define what do you mean by "becomes broken".

    .NET framework uses Unicode natively since .NET v1.0, shouldn't have any problem on handling Unicode string except on the case of Console, because the problem is in Console subsystem of Windows.

    Btw, I'm using English version of VS2015, on English version of Win7 SP1 x64, and the following code display without issue on "Debug" of "Output" windows in the IDE.

    System.Diagnostics.Debug.WriteLine("안녕하세요");


    Wednesday, February 28, 2018 1:43 AM
    Answerer
  • Hello.

    The picture below shows the broken korean string. Other korean letters that was made in xamel code in design time shows without the problem . Broken korean were made in code behind by code, lblincludeFoder.Content = "일치 단어 개수 : " + Convert.ToString(count); .

    thanks.


    • Edited by leetoya Wednesday, February 28, 2018 1:53 AM
    Wednesday, February 28, 2018 1:52 AM
  • Hi,

    I cannot read/write Korean language and Hangul characters.
    "Consolas" is a font family for Latin, Greek, Cyrillic, Armenian, not supports Hangul characters. 


    You need to use a font that supports Hangul characters.

    Regards,

    Ashidacchi

    Wednesday, February 28, 2018 1:56 AM
  • What is the type of lblincludeFoder control?

    Btw, AFAIK, all controls derived from "Microsoft Common Control Library 6" (i.e.: those ordinary Windows control) are Unicode aware.


    I've also checked the default Label control in WPF can display the string without problem.
    Wednesday, February 28, 2018 2:05 AM
    Answerer
  • Thanks for replies.

    The type of lblincludeFoder control is label.

    Also, I tried my different font but it does not help.

    Regards

    Wednesday, February 28, 2018 4:21 AM
  • Humm... my WPF project also use that but can display the fonts correctly.

    The test project targets .NET 4.5.2, does not have Neutral Language be set on "Assembly Information", and do not set FontFamily on any of the controls.

    Btw, try dump the value of lblincludeFoder.Content after it has been set to Output window to make sure the string itself is okay at that point.

    And you may also post the XAML declaration of that label here and see if there are anything related. I see Korean displays correctly for other controls on your form.

    Wednesday, February 28, 2018 4:28 AM
    Answerer
  • Since WPF controls are rendered with GDI+ string drawing functions, if the character is not found on specified font, it'll be automatically fall back on "fallback font" specified by the system. (Or in case no applicable fallback font is found, some variant of "?" will be displayed, instead of garbage characters)

    In fact, the WPF application I used for testing originally uses "Noto Sans Javanese" font that does not have Hangul characters in it, but still render the text correctly.


    Wednesday, February 28, 2018 4:40 AM
    Answerer
  • I attached full code with xaml.

    <TextBox x:Name="txtSearch" TextChanged="txtSearch_TextChanged" Grid.Row="1" Grid.Column="1" Width="175"/>

    public void textChange()
            {
                if (rdAllPath || rdUserSelected && check1)
                {
                    strDisting = txtSearch.Text;
                    count = FindListViewItem(includeFolderListview, includeFolderListview.Items.Count);
                    if (count == 0)
                        lblincludeFoder.Content = "";
                    else
                        lblincludeFoder.Content = "일치 단어 개수: " + Convert.ToString(count);
                }

    }

    public int FindListViewItem(ListView listview, int numOfItem)
            {
                int total_count = 0;

                // scroll to bottom for the list to make sure every item is in the view
                if (listview.Items.Count > 1) listview.ScrollIntoView(listview.Items[listview.Items.Count - 1]);
                 
                int childCount = VisualTreeHelper.GetChildrenCount(listview);
                for (int i = 0; i < childCount ; i++)
                {               
                    DependencyObject listBoxChrome = VisualTreeHelper.GetChild(listview, i);
                    List<TextBlock> textBlockArray = new List<TextBlock>();
                    textBlockArray = findChildren<TextBlock>(listBoxChrome, "textBlock", numOfItem, textBlockArray);
                    foreach (TextBlock textBlock in textBlockArray)
                    {
                        count = changeTextColour(textBlock, strDisting, chCapital);
                        total_count = total_count + count;
                    }
                }

                // scroll back to top to make sure everything looks correct
                if (listview.Items.Count > 1) listview.ScrollIntoView(listview.Items[0]);

                return total_count;
            }

            //Check all the children of parent object match with childName and return to return array
            public static List<T> findChildren<T>(DependencyObject depObj, string childName, int numOfType, List<T> returnArray) where T : DependencyObject
            {          
                // Confirm obj is valid.
                if (depObj == null) return returnArray;
                // success case
                if (depObj is T)
                {
                    if (((FrameworkElement)depObj).Name == childName)
                    {
                        returnArray.Add(depObj as T);                  
                    }  
                }               
               
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

                    //DFS
                    returnArray = findChildren<T>(child, childName, numOfType, returnArray);
                    if (returnArray.Count == numOfType)
                        return returnArray;
                }
                return returnArray;
            }

            private int changeTextColour(TextBlock textBlock, String searchString, Boolean ignoreCase) {
                int count = 0;
                string strTemp = searchString;
                string temp = string.Empty;
                foreach (char c in strTemp.ToArray())
                {
                    temp += c.ToString().Replace("*", "[*]").Replace(".", "\\.").Replace("+", "[+]").Replace("$", "[$]").Replace("|", "[|]").Replace("?", "[?]").Replace("(", "\\(").Replace(")", "\\)").Replace(@"\", @"\\").Replace("{", "\\{").Replace("}", "\\}").Replace("^", "\\^").Replace("[", "\\[").Replace("]", "\\]").Replace("\"", @"\u0022");
                }
                strTemp = temp;


                if (chCapital)
                    regex = new Regex("(" + strTemp + ")");
                else
                    regex = new Regex("(" + strTemp + ")", RegexOptions.IgnoreCase);

                string textBoxString = textBlock.Text;
                textBlock.Inlines.Clear();          

                if (strTemp.Length == 0)
                {
                    textBlock.Inlines.Add(textBoxString);
                    return count;
                }

                string[] substrings = regex.Split(textBoxString);
                foreach (var item in substrings)
                {
                    if (regex.Match(item).Success)
                    {
                        count = count + 1;
                        Run runx = new Run(item);
                        runx.Background = Brushes.LightGreen;
                        textBlock.Inlines.Add(runx);                   
                    }
                    else
                    {   textBlock.Inlines.Add(item);}
                }

                return count;
         }

    Wednesday, February 28, 2018 4:59 AM
  • The problem is that string in korean does not read as the picture I attached above

    Wednesday, February 28, 2018 5:01 AM
  • 1) Make your code behind file as your current document.
    2) "File" -> "Advanced Save Options".
    3) Try select "Unicode (UTF-8 with signature) - Codepage 65001" here.
    4) Save the code behind file again.
    5) Recompile.

    I suspect the string you try to use is broken in source code level. Better just save them in UTF-8.

    Thursday, March 1, 2018 1:29 AM
    Answerer
  • Hello Zhanglong Wu.

    Thanks for reply.

    My current save option is Unicode (UTF-8 with signature) - Codepage 65001.

    But it cannot read korean.

    Monday, March 5, 2018 6:50 AM
  • Check the code file where the problematic string come from then.

    From your screen dump, it appears that not all Korean characters have display problem, so the root cause should be "some unicode string incorrectly be read as ansi string therefore becomes corrupted".

    Monday, March 5, 2018 4:11 PM
    Answerer