UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> YourFunctionName    


Change the case of text to Sentence Case. An example:

  strText = "this is a sentence. it'll undergo SentenceCaseRE! does it work? let me know."
  result = SentenceCaseRE(strText)
  debug.print results 'prints: This is a sentence. It'll undergo SentenceCaseRE! Does it work? Let me know."
' YourFunctionName
' http://www.utteraccess.com/wiki/YourFunctionName
' 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                      
'  Change the case of a string to sentence case.
'     (Capitalize the first character of a sentence.)
'  strText - the sting to be modified
'  A string in which the first character of sentences are capitalized.
'  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)))
     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
YourFunctionName 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 1,856 times.  This page was last modified 14:13, 15 October 2015 by azolder.   Disclaimers