UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> SentenceCaseRE    


Synopsis

Function Changes the case of text to Sentence Case. The first word of a sentence is capitalize. Capitalization of words inside the sentence remains unchanged.

CODE
' SentenceCaseRE
' http://www.utteraccess.com/wiki/SentenceCaseRE
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2015-10-16                    
'
'**********************************
'SentenceCaseRE
'  Change the case of a string to sentence case.
'     (Capitalize the first character of a sentence.)
' ARGUMENTS
'  strText - the sting to be modified
' RETURNS
'  A string in which the first character of sentences are capitalized.
' NOTES
'  By definition, sentences are terminated with periods, exclamation marks
'     or question marks followed by some white space. Any uncapitalized
'     letters (a-z) are capitalized.
'  The first character in the sting is capitalized.
Function SentenceCaseRE(strText As String) As String
  Dim RE As Object
  Dim temp As String
  Dim results As Variant
  Dim i As Long
 
  temp = strText
  'Set RE = New RegExp
  Set RE = CreateObject("VBScript.RegExp")
  With RE
     '.IgnoreCase = True
     .Global = True
     .MultiLine = True
     .Pattern = "([.!?])\s+[a-z]"
     If .Test(temp) Then
        'find all items to be changed
        Set results = .Execute(temp)
        For i = 0 To results.Count - 1
           'change each occurence
           .Pattern = "\" & results.Item(i)
           temp = .Replace(temp, UCase(results.Item(i)))
        Next
     End If
  End With
  'now Ucase first char in strTemp
  Mid(temp, 1, 1) = UCase(Left(temp, 1))
  SentenceCaseRE = temp
End Function

Creative Commons License
SentenceCaseRE by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 766 times.  This page was last modified 02:35, 17 October 2015 by azolder.   Disclaimers