C# Capitalizing string, but only after certain punctuation marks

老子叫甜甜 提交于 2019-12-13 05:44:39

问题


I'm trying to find an efficient way to take an input string and capitalize the first letter after every punctuation mark (. : ? !) which is followed by a white space.

Input:

"I ate something. but I didn't: instead, no. what do you think? i think not! excuse me.moi"

Output:

"I ate something. But I didn't: Instead, no. What do you think? I think not! Excuse me.moi"

The obvious would be to split it and then capitalize the first char of every group, then concatenate everything. But it's uber ugly. What's the best way to do this? (I'm thinking Regex.Replace using a MatchEvaluator that capitalizes the first letter but would like to get more ideas)

Thanks!


回答1:


Try this:

string expression = @"[\.\?\!,]\s+([a-z])";
string input = "I ate something. but I didn't: instead, no. what do you think? i think not! excuse me.moi";
char[] charArray = input.ToCharArray();
foreach (Match match in Regex.Matches(input, expression,RegexOptions.Singleline))
{
    charArray[match.Groups[1].Index] = Char.ToUpper(charArray[match.Groups[1].Index]);
}
string output = new string(charArray);
// "I ate something. But I didn't: instead, No. What do you think? I think not! Excuse me.moi"



回答2:


Fast and easy:

static class Ext
{
    public static string CapitalizeAfter(this string s, IEnumerable<char> chars)
    {
        var charsHash = new HashSet<char>(chars);
        StringBuilder sb = new StringBuilder(s);
        for (int i = 0; i < sb.Length - 2; i++)
        {
            if (charsHash.Contains(sb[i]) && sb[i + 1] == ' ')
                sb[i + 2] = char.ToUpper(sb[i + 2]);
        }
        return sb.ToString();
    }
}

Usage:

string capitalized = s.CapitalizeAfter(new[] { '.', ':', '?', '!' });



回答3:


I use an extension method.

public static string CorrectTextCasing(this string text)
{
    //  /[.:?!]\\s[a-z]/ matches letters following a space and punctuation,
    //  /^(?:\\s+)?[a-z]/  matches the first letter in a string (with optional leading spaces)
    Regex regexCasing = new Regex("(?:[.:?!]\\s[a-z]|^(?:\\s+)?[a-z])", RegexOptions.Multiline);

    //  First ensure all characters are lower case.  
    //  (In my case it comes all in caps; this line may be omitted depending upon your needs)        
    text = text.ToLower();

    //  Capitalize each match in the regular expression, using a lambda expression
    text = regexCasing.Replace(text, s => (s.Value.ToUpper));

    //  Return the new string.
    return text;

}

Then I can do the following:

string mangled = "i'm A little teapot, short AND stout. here IS my Handle.";
string corrected = s.CorrectTextCasing();
//  returns "I'm a little teapot, short and stout.  Here is my handle."



回答4:


Using the Regex / MatchEvaluator route, you could match on

"[.:?!]\s[a-z]"

and capitalize the entire match.




回答5:


Where the text variable contains the string

        string text = "I ate something. but I didn't: instead, no. what do you think? i think not! excuse me.moi";
        string[] punctuators = { "?", "!", ",", "-", ":", ";", "." };
        for (int i = 0; i< 7;i++)
        {
            int pos = text.IndexOf(punctuators[i]);
            while(pos!=-1)
            {
                text = text.Insert(pos+2, char.ToUpper(text[pos + 2]).ToString());
                text = text.Remove(pos + 3, 1);
                pos = text.IndexOf(punctuators[i],pos+1);
            }
        }


来源:https://stackoverflow.com/questions/5664286/c-sharp-capitalizing-string-but-only-after-certain-punctuation-marks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!