Results 1 to 4 of 4

Thread: VB Code for sentence cap

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    2

    VB Code for sentence cap

    I'm trying to create a program that allows the user to type multiple sentences and after he clicks the button the program will capatilize the first letter of each sentence. I have done this but the sentences must have a double space after the period to work. I want the program to work even if the user only uses a single space after the period. Here is the code I have so far.

    Dim strString As String
    Dim searchStrTxt As Integer
    Dim strCmpare As String
    Dim strRplc As String
    Dim strModified As String
    Dim strFirstChr As String

    strString = TextBox1.Text

    strModified = strString
    strFirstChr = UCase(Mid$(strModified, 1, 1))
    strModified = strFirstChr & Mid$(strModified, 2)


    'Search for single-spaced punctuation
    For searchStrTxt = 1 To Len(strString) - 1
    'DoEvents
    strCmpare = Mid$(strModified, searchStrTxt, 3)
    If strCmpare = ". " Then
    strRplc = Mid$(strString, searchStrTxt, 4)
    strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    End If

    If strCmpare = "? " Then
    strRplc = Mid$(strString, searchStrTxt, 4)
    strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    End If

    If strCmpare = "! " Then
    strRplc = Mid$(strString, searchStrTxt, 4)
    strModified = Replace$(strModified, strRplc, UCase$(strRplc))
    End If
    Next searchStrTxt

    'Assign new string
    TextBox1.Text = strModified

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB Code for sentence cap

    Thread moved from the FAQ forum (which is not the place to post your questions) to the 'VB.Net' forum (based on your previous post being VB2008).

  3. #3
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: VB Code for sentence cap

    I'd suggest looking for the periods rather the spacings or both together. As written languages are riddled with spaces. While the period only signifies the end of a sentence.

    You can find the position of any ".", "!", "?" using IndexOf. Using that position work to your right in a loop skipping any spaces that fallow until you find your first letter. Which when you do, you can simply replace it with it's capitalized equivalent. Once complete, you now know to look for your next period and to repeat the process all over again.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: VB Code for sentence cap

    you're using a lot of legacy code there. try this:

    vb Code:
    1. Private Function capitalizeSentences(ByVal s As String) As String
    2.  
    3.     Dim sentences() As String = s.Split(New String() {". ", "? ", "! ", Chr(10) & Chr(10), "." & Chr(10), "?" & Chr(10), "!" & Chr(10), Environment.NewLine & Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    4.     Dim newSentences() As String = DirectCast(sentences.Clone, String())
    5.  
    6.     Dim searchStart As Integer = 0
    7.     For x As Integer = 0 To sentences.GetUpperBound(0)
    8.  
    9.         sentences(x) = sentences(x).Trim
    10.         newSentences(x) = newSentences(x).Trim
    11.         newSentences(x) = newSentences(x).Substring(0, 1).ToUpper & newSentences(x).Substring(1)
    12.  
    13.         Dim start As Integer = s.IndexOf(sentences(x), searchStart)
    14.         For c As Integer = start To start + sentences(x).Length - 1
    15.             s = s.Remove(c, 1)
    16.             s = s.Insert(c, newSentences(x).Substring(c - start, 1))
    17.         Next
    18.         searchStart += sentences(x).Length + 2
    19.  
    20.     Next
    21.  
    22.     Return s
    23.  
    24. End Function

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width