Capitalizing words in a string using c#

前端 未结 10 1333
攒了一身酷
攒了一身酷 2020-12-16 11:22

I need to take a string, and capitalize words in it. Certain words (\"in\", \"at\", etc.), are not capitalized and are changed to lower case if encountered. The first word s

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 12:17

    A non-clever approach that handles the simple case:

    var s = "of mice and men By CNN";
    var sa = s.Split(' ');
    for (var i = 0; i < sa.Length; i++)
        sa[i] = sa[i].Substring(0, 1).ToUpper() + sa[i].Substring(1);
    var sout = string.Join(" ", sa);
    Console.WriteLine(sout);
    

提交回复
热议问题