Does anyone have a good Proper Case algorithm

前端 未结 13 1212
一生所求
一生所求 2020-12-15 04:03

Does anyone have a trusted Proper Case or PCase algorithm (similar to a UCase or Upper)? I\'m looking for something that takes a value such as \"GEORGE BURDELL\"

13条回答
  •  粉色の甜心
    2020-12-15 05:02

    Unless I've misunderstood your question I don't think you need to roll your own, the TextInfo class can do it for you.

    using System.Globalization;
    
    CultureInfo.InvariantCulture.TextInfo.ToTitleCase("GeOrGE bUrdEll")
    

    Will return "George Burdell. And you can use your own culture if there's some special rules involved.

    Update: Michael (in a comment to this answer) pointed out that this will not work if the input is all caps since the method will assume that it is an acronym. The naive workaround for this is to .ToLower() the text before submitting it to ToTitleCase.

提交回复
热议问题