1

我正在尝试将所有列表项转换为 TitleCase。我认为 TitleCase 会很简单,但它似乎与大写或小写不一样。有任何想法吗?这适用于上下:

列表 myList = new List() { "abc", "DEF", "Def", "aBC" };

myList = myList.ConvertAll(x => x.ToUpper());
myList = myList.ConvertAll(x => x.ToLower());

但这些都不起作用:

myList = myList.ConvertAll(x => x.ToTitleCase());
myList = myList.ConvertAll(x => x.TitleCase());
4

3 回答 3

5

使用TextInfo.ToTitleCase方法。

List<string> myList = new List<string>() { "abc", "DEF", "Def", "aBC" };
CultureInfo currentCulture =  System.Threading.Thread.CurrentThread.CurrentCulture;
myList = myList.Select(r=> currentCulture.TextInfo.ToTitleCase(r.ToLower())).ToList();

输出:

foreach (string str in myList)
    Console.WriteLine(str);

结果:

Abc
Def
Def
Abc

编辑:

你可以使用 ConvetAll 像:

myList =  myList.ConvertAll(r => currentCulture.TextInfo.ToTitleCase(r.ToLower()));
于 2012-12-18T06:51:31.887 回答
1
 class Program
    {
        static void Main(string[] args)
        {

            List<string> myList = new List<string>() { "abc", "DEF", "Def", "aBC" };

            CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
            myList=myList.ConvertAll(r => currentCulture.TextInfo.ToTitleCase(r.ToLower()));
            int i=0;
            myList.ForEach(delegate(string k)
            {
                 Console.WriteLine(k);
                 i++;
            });
            Console.ReadLine();
        }
    }
于 2012-12-18T07:07:29.610 回答
1

是的,如果原始值是大写的,则必须降低。您可能还需要添加全球化命名空间。我在 DropDownList_DataBound 事件中使用了它。

CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;        
foreach (ListItem li in DropDownList.Items)
{
     li.Text = currentCulture.TextInfo.ToTitleCase(li.Text.ToLower());
}
于 2014-04-02T17:16:58.980 回答