|
-
Nov 15th, 2011, 02:56 PM
#1
Thread Starter
New Member
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
-
Nov 15th, 2011, 03:33 PM
#2
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).
-
Nov 15th, 2011, 03:39 PM
#3
Hyperactive Member
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.
-
Nov 15th, 2011, 03:54 PM
#4
Re: VB Code for sentence cap
you're using a lot of legacy code there. try this:
vb Code:
Private Function capitalizeSentences(ByVal s As String) As String
Dim sentences() As String = s.Split(New String() {". ", "? ", "! ", Chr(10) & Chr(10), "." & Chr(10), "?" & Chr(10), "!" & Chr(10), Environment.NewLine & Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Dim newSentences() As String = DirectCast(sentences.Clone, String())
Dim searchStart As Integer = 0
For x As Integer = 0 To sentences.GetUpperBound(0)
sentences(x) = sentences(x).Trim
newSentences(x) = newSentences(x).Trim
newSentences(x) = newSentences(x).Substring(0, 1).ToUpper & newSentences(x).Substring(1)
Dim start As Integer = s.IndexOf(sentences(x), searchStart)
For c As Integer = start To start + sentences(x).Length - 1
s = s.Remove(c, 1)
s = s.Insert(c, newSentences(x).Substring(c - start, 1))
Next
searchStart += sentences(x).Length + 2
Next
Return s
End Function
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|