gpt4 book ai didi

c# - 在 C# 中比较日语字符

转载 作者:太空狗 更新时间:2023-10-30 00:18:29 32 4
gpt4 key购买 nike

我正在检查日文字符串中的空格并将其替换为“_”。这就是我正在做的:

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

这是这段代码的输出

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

它在简单的英文字符串中用“_”替换了空格,但在日文字符串中却没有。

最佳答案

因为你的 input2 中的 look-like-space 并不是一个真正的 space ,只需检查它的 ascii 码

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的原解释:

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)

关于c# - 在 C# 中比较日语字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35764951/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com