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.
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)
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()));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With