.NET method to convert a string to sentence case

2020-01-27 06:04发布

I'm looking for a function to convert a string of text that is in UpperCase to SentenceCase. All the examples I can find turn the text into TitleCase.

Sentence case in a general sense describes the way that capitalization is used within a sentence. Sentence case also describes the standard capitalization of an English sentence, i.e. the first letter of the sentence is capitalized, with the rest being lower case (unless requiring capitalization for a specific reason, e.g. proper nouns, acronyms, etc.).

Can anyone point me in the direction of a script or function for SentenceCase?

9条回答
劫难
2楼-- · 2020-01-27 06:35

This is what I use (VB.NET). It works in most situations, including:

  • multiple sentences
  • sentences beginning and ending with spaces
  • sentences beginning with characters other than A-Z. For example it will work for: "if you want $100.00 then just ask me".

    <Extension()>
    Public Function ToSentanceCase(ByVal s As String) As String
        ' Written by Jason. Inspired from: http://www.access-programmers.co.uk/forums/showthread.php?t=147680
    
        Dim SplitSentence() As String = s.Split(".")
    
        For i = 0 To SplitSentence.Count - 1
            Dim st = SplitSentence(i)
    
            If st.Trim = "" Or st.Trim.Count = 1 Then Continue For ' ignore empty sentences or sentences with only 1 character.
    
            ' skip past characters that are not A-Z, 0-9 (ASCII) at start of sentence.
            Dim y As Integer = 1
            Do Until y > st.Count
                If (Asc(Mid(st, y, 1)) >= 65 And Asc(Mid(st, y, 1)) <= 90) Or _
                      (Asc(Mid(st, y, 1)) >= 97 And Asc(Mid(st, y, 1)) <= 122) Or _
                     (Asc(Mid(st, y, 1)) >= 48 And Asc(Mid(st, y, 1)) <= 57) Then
                    GoTo Process
                Else
                    Dim w = Asc(Mid(st, y, 1))
                    y += 1
                End If
            Loop
            Continue For
    
    Process:
            Dim sStart As String = ""
            If y > 1 Then sStart = Left(st, 0 + (y - 1))
    
            Dim sMid As String = UCase(st(y - 1)) ' capitalise the first non-space character in sentence.
    
            Dim sEnd As String = Mid(st, y + 1, st.Length)
    
            SplitSentence(i) = sStart & sMid & sEnd
    
        Next
    
        ' rejoin sentances back together:
        Dim concat As String = ""
        For Each st As String In SplitSentence
            concat &= st & "."
        Next
    
        concat = concat.TrimEnd(1)
    
        Return concat
    
    End Function
    

But as for proper nouns and acronyms, well... there are always going to be situations in the English language where punctuation is not as simple. For example this script won't detect an ellipsis ("..."), or abbreviations (eg: "Mr. Jones lived on Magnolia Blvd. near Chris' house").

To address the problem completely you will need to produce a dictionary of all the possible abbreviations/punctuation's for the language, and keep the dictionary up-to-date! After considering this most will be happy with a compromise, otherwise just use Microsoft Word.

查看更多
欢心
3楼-- · 2020-01-27 06:42

This works for me.

/// <summary>
/// Converts a string to sentence case.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>A string</returns>
public static string SentenceCase(string input)
{
    if (input.Length < 1)
        return input;

    string sentence = input.ToLower();
    return sentence[0].ToString().ToUpper() +
       sentence.Substring(1);
}
查看更多
地球回转人心会变
4楼-- · 2020-01-27 06:44

If you'd like to sentence case a string containing punctuation other than just periods:

string input = "THIS IS YELLING! WHY ARE WE YELLING? BECAUSE WE CAN. THAT IS ALL.";
var sentenceRegex = new Regex(@"(^[a-z])|[?!.:,;]\s+(.)", RegexOptions.ExplicitCapture);
input = sentenceRegex.Replace(input.ToLower(), s => s.Value.ToUpper());
查看更多
登录 后发表回答