gpt4 book ai didi

c# - 如何忽略 String.ToUpper() 中的撇号?

转载 作者:行者123 更新时间:2023-11-30 19:08:38 26 4
gpt4 key购买 nike

在法国,很多城市的名字中都有撇号。就像“rue de l'église”

我们几乎在每个 UI 部分都使用转换器以完整的大写形式编写它。

但是 string.ToUpper 似乎有一个错误,因为我们得到的是“RUE DE L'église”而不是我们应该得到的“RUE DE L'ÉGLISE”。

你能解释一下为什么吗?无论如何获得预期的结果?

我的转换器是这样的

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
var res = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value.ToString().ToUpper());
return res;
}

return String.Empty;
}

最佳答案

ToTitleCase() 没有按照您的意愿进行。它将每个单词的第一个字符大写。你想要的只是普通的 string.ToUpper():

Console.WriteLine("rue de l'église".ToUpper());

输出:

RUE DE L'ÉGLISE

ToTitleCase():

Console.WriteLine(CultureInfo.GetCultureInfo("fr-fr").TextInfo.ToTitleCase("rue de l'église"));

输出

Rue De L'église

结合使用 ToTitleCase()ToUpper() 会导致您描述的这种奇怪的行为,因为 ToTitleCase() 会尝试将所有其他字符小写比第一个(根据 documentation ,除了全部大写且被视为首字母缩略词的单词外)

关于c# - 如何忽略 String.ToUpper() 中的撇号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019150/

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