Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Japanese Characters in C#

Tags:

string

c#

I am checking a Japanese string for spaces and replace them with "_". This is what I am doing:

string input1="abc  dfg";
string input2="尾え れ";
if(input1.Contains(" "))
{
  Console.WriteLine(input1.Replace(" ","_"));
}
Console.WriteLine("------------------");
if(input2.Contains(" "))
{
  Console.WriteLine(input2.Replace(" ","_"));
}

Here is the Output on this code

abc__dfg
------------------

It replaces spaces with "_" in the simple English string but in the Japanese string it does not.

like image 461
Addy Avatar asked Dec 24 '22 08:12

Addy


2 Answers

because the look-like-space in your input2 is not really a space, just check the ascii code of it

Console.WriteLine(Convert.ToInt32(' ')); // output: 12288
Console.WriteLine(Convert.ToInt32(' ')); // output: 32

string input1 = "abc  dfg";
string input2 = "尾え れ"; // a space
string input3 = "尾え れ"; // not a space
if (input1.Contains(" "))
{
    Console.WriteLine(input1.Replace(" ", "_"));
}
Console.WriteLine("------------------");
if (input2.Contains(" "))
{
    Console.WriteLine(input2.Replace(" ", "_"));
}
Console.WriteLine("------------------");
if (input3.Contains(" "))
{
    Console.WriteLine(input3.Replace(" ", "_"));
}

@Ronan Thibaudau's original explanation:

Because it's not a space, it's not the same character, copy what you call a "space" from the input 2 string and paste it in the input2.replace method and it will work, it's just not the same character as the space you typed (even when i try to select it here on stackoverflow it's twice as large as the spaces in your input1 so it can't be the same character)

like image 96
Kien Chu Avatar answered Jan 10 '23 11:01

Kien Chu


If you don't want to worry yourself with ASCII code or copy-pasting characters you don't know how to expect, just do something like this:

//using System.Linq;
string input1 = "abc  dfg";
string input2 = "尾え れ";
if (input1.Any(char.IsWhiteSpace))
{

   Console.WriteLine(new string(input1.Select(x=> char.IsWhiteSpace(x) ? '_' : x).ToArray()));
}
Console.WriteLine("------------------");
if (input2.Any(char.IsWhiteSpace))
{

     Console.WriteLine(new string(input2.Select(x => char.IsWhiteSpace(x) ? '_' : x).ToArray()));
}
like image 38
Tyress Avatar answered Jan 10 '23 09:01

Tyress