Jump to content

Recommended Posts

Posted (edited)

This is a spin-off of >Seeker's function which i rewrote and tried to optimize for doing only sentence casing and for better Unicode handling. Haven't done a lot of testing with it, but it seems to work well so far.

#include-once
; #FUNCTION# ====================================================================================================================
; Name ..........: _SentenceCase
; Description ...: Capitalize the first letter of sentences.
; Syntax ........: _SentenceCase(Byref $sString)
; Parameters ....: $sString: [in/out] A string value.
; Return values .: Success: A string is returned.
;                  Failure: Sets @error = 1 and returns 0.
; Author ........: iCode
; Modified ......: 30-MAR-2015
; Remarks .......:
; Related .......: http://www.autoitscript.com/forum/topic/147086-udf-for-title-case-initial-caps-and-sentence-case/
; Link ..........: http://www.autoitscript.com/forum/topic/169290-sentence-case-capitalize-first-letter-of-sentences/
; Example .......: No
; ===============================================================================================================================
Func _SentenceCase(ByRef $sString)

    Local $aStr = StringRegExp($sString, "(*UCP)(?s)[[:alpha:]].+?(?:[.?!:;]|\z)\s*|[^[:alpha:]]+", 3)
    If @error Then Return SetError(1, 0, 0)

    Local $sChar, $sRet = ""
    For $i = 0 To UBound($aStr) - 1
        If StringRegExp($aStr[$i], "(*UCP)(?s)^[[:alpha:]]+.*?[.?!:;]") Then
            $sChar = StringLeft($aStr[$i], 1)
            If StringIsLower($sChar) Then
                $aStr[$i] = StringUpper($sChar) & StringTrimLeft($aStr[$i], 1)
            EndIf
        EndIf
        $sRet &= $aStr[$i]
    Next

    Return $sRet

EndFunc

CHANGE LOG:
28-MAR-2015 - initial version

30-MAR-2015 - added more sentence terminition characters ( ; : ) - should probably get these from a variable?

Edited by iCode

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Posted

I found for these sentences, "wait a minute! where are the capitalize first letters?", to work,

If StringRegExp($aStr[$i], "(*UCP)(?s)^[[:alpha:]]+.*?\.") Then

has to be changed to this,

If StringRegExp($aStr[$i], "(*UCP)(?s)^[[:alpha:]]+.*?[?!.]") Then

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...