4

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.

5
  • 8
    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) Mar 3, 2016 at 6:49
  • @RonanThibaudau how could you notice that? ;)
    – Ian
    Mar 3, 2016 at 6:50
  • 2
    @Ian Was my immediate intuition reading the question so started selecting it to paste it and check the actual data, noticed it was twice as wide (two spaces in first string vs a single char in second strong both totalling the same width) Mar 3, 2016 at 6:52
  • 2
    @RonanThibaudau you should put that as an answer actually! :D
    – Ian
    Mar 3, 2016 at 6:53
  • @RonanThibaudau thanks I got it :)
    – AddyProg
    Mar 3, 2016 at 6:56

4 Answers 4

9

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)

2
  • 2
    A clear explanation is there in comment by Ronan Thibaudau Let him place the answer. or mention him in your answer. I think that could be more appreciable. Mar 3, 2016 at 6:57
  • @un-lucky: I included his comment in my answer
    – Kien Chu
    Mar 3, 2016 at 6:59
1

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()));
}
0

Use this piece of code for the second string and it will work . Tested and it is returning the correct output .

    if (input2.Contains(string.Empty))
    {
        string cleanedString = System.Text.RegularExpressions.Regex.Replace(input2, @"\s+", "_");
        Console.WriteLine(cleanedString);
    }
0

Most likely your Console font does not support and/or the (default) code page does not support the Japanese characters.

Try

     Console.WriteLine(Console.OutputEncoding.EncodingName);
     Console.WriteLine(Console.OutputEncoding.CodePage);
     Console.WriteLine(input2);
     Debug.Write(input2);

for comparison. Select a font and codepage which supports Japanese characters, e.g.

Console.OutputEncoding = Encoding.UTF8;

In order to change the default codepage of your Console, please check this answer: Unicode characters in Windows command line - how?

Regarding the string itself: Copy/Paste your string 尾え れ to this side: Unicode code converter. The Unicode codepoints are U+5C3E U+3048 U+3000 U+308C

U+3000 is an Ideographic Space, not the "normal" space U+0020.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.