'-------------------------------------------------------------------------------------------------------------------------------
' This program demonstrates the code to extract individual words.
'-------------------------------------------------------------------------------------------------------------------------------
Public Class Form1
Private Sub cmdGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdGo.Click
Dim line As String
Dim word As String = ""
Dim ctr As Integer, wordCtr As
Integer
ctr = 0 ' character pointer; must start at 0 for 0 index
wordCtr = 0
Dim outputString As String = ""
line = txtInput.Text
Debug.WriteLine("Text
entered is " & line) ' echo line
'---------------------------------------------------------------------------------
' This module counts the number of words in a string by counting the
' number of blanks.
'---------------------------------------------------------------------------------
Dim blankPosition As
Integer, beginning As Integer, wordCount As Integer
wordCount = 0
beginning = 0
blankPosition = 0
While
blankPosition >= 0
' skip any leading or embedded spaces
While line.Substring(beginning, 1) = " "
beginning = beginning + 1
End While
' find the
position of the next blank
blankPosition = line.IndexOf(" ", beginning)
Debug.WriteLine("Blank found in position " & blankPosition)
wordCount = wordCount + 1
beginning = blankPosition + 1
End While
'---------------------------------------------------------------------------------
' End of module to count
words
'---------------------------------------------------------------------------------
Debug.WriteLine("Number of words is " & wordCount)
While wordCtr < wordCount
'---------------------------------------------------------------------------------
' Module to
extract the next word from a string, and shift the pointer
' to the
beginning of the next word.
'---------------------------------------------------------------------------------
Dim
nextBlankPosition As Integer
' remove hyphens
line =
line.Replace("-", " ")
' locate end of word
Do
' find next blank
nextBlankPosition = line.IndexOf(" ", ctr)
' handle final end of sentence
If (nextBlankPosition = -1) Then
nextBlankPosition =
line.Length
End If
' skip embedded
blanks
If (nextBlankPosition = ctr) Then
ctr = ctr + 1
Debug.WriteLine("Skip to " & beginning)
End If
Loop Until
nextBlankPosition > ctr
' extract word
word = line.Substring(ctr,
nextBlankPosition - ctr)
ctr = nextBlankPosition + 1 ' move pointer to beginning of next word
'-----------------------------------------------------------------------
' Module to
trim leading punctuation and spaces
'-----------------------------------------------------------------------
Dim firstChar
As String
Dim
isAlphaCharacter As Boolean
' as long as first
character is not alpha, strip it
Do
' copy first character of word
firstChar =
word.Substring(0, 1)
' check to see if first character is alpha
isAlphaCharacter =
Asc(firstChar) >= 65 _
And Asc(firstChar) <= 90 _
Or Asc(firstChar) >= 97 _
And Asc(firstChar) <= 122
' if first character is not alpha, strip it by extracting
' all characters to the right of the first character
If Not isAlphaCharacter Then
word =
word.Substring(1)
End If
Loop Until
isAlphaCharacter
'-----------------------------------------------------------------------
' End
trim-leading-punctuation module
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
' Module to
trim trailing punctuation and spaces
'-----------------------------------------------------------------------
Dim lastChar
As String
' as long as last
character is not alpha, strip it
Do
' copy last character of word
lastChar =
word.Substring(word.Length - 1)
' check to see if last character is alpha
isAlphaCharacter = Asc(lastChar)
>= 65 _
And Asc(lastChar) <= 90 _
Or Asc(lastChar) >= 97 _
And Asc(lastChar) <= 122
' if last character is not alpha, strip it by extracting
' all characters to the left of the last character
If Not isAlphaCharacter Then
word =
word.Substring(0, word.Length - 1)
End If
Loop Until isAlphaCharacter
'-----------------------------------------------------------------------
' End
trim-leading-punctuation module
'-----------------------------------------------------------------------
'------------------------------------------------------------------------
' End of
module to getWord
'------------------------------------------------------------------------
outputString
&= "-" & word & "-" & vbCrLf
wordCtr = wordCtr + 1
End While
txtOutput.Text =
outputString
Debug.WriteLine("ALL DONE")
End Sub
' This event procedure
runs as the form is loading, and initializes
' the input text box
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
txtInput.Text = " The old dog, named
""Peewee"", ate a lot. Still, we all loved her...."
End Sub
End Class
'================================================================
' Version with subs and
functions
Public Class Form1
Private Sub cmdGo_Click(ByVal sender As System.Object,
_
ByVal e As System.EventArgs) _
Handles cmdGo.Click
Dim line As String
Dim word As String = ""
Dim ctr As Integer,
wordCtr As Integer
ctr = 0 ' character
pointer; must start at 0 for 0 index
wordCtr = 0
line = " The old dog,
named ""Peewee"", ate a lot. Still, we all loved her...."
' remove leading and
trailing spaces
' not needed; code below
handles it
'line = Trim(line)
MsgBox(line) ' echo line
' Count number of words
in sentence and then echo it
Dim wordCount As Integer
= countWords(line)
MsgBox("Number of words
is " & wordCount)
While wordCtr < wordCount
Call getWord(line, word, ctr)
MsgBox("word = " & word)
wordCtr = wordCtr + 1
End While
MsgBox("ALL DONE")
End Sub
'---------------------------------------------------------------------------------
' Return the next word from a string, along with a
pointer to the beginning of
' the next word.
'---------------------------------------------------------------------------------
Private Sub getWord(ByVal newString As String, _
ByRef word As String, _
ByRef beginning As
Integer)
Dim blankPosition As
Integer
' remove hyphens
newString =
newString.Replace("-", " ")
' locate end of word
Do
' find next blank
blankPosition = newString.IndexOf(" ", beginning)
' handle final end of sentence
If (blankPosition = -1) Then
blankPosition = newString.Length
End If
' skip embedded blanks
If (blankPosition = beginning) Then
beginning = beginning + 1
'MsgBox("Skip to " & beginning)
End If
Loop Until blankPosition
> beginning
' extract word
word =
newString.Substring(beginning, blankPosition - beginning)
beginning = blankPosition
+ 1 ' move pointer to beginning of next word
word =
trimPunctuationLeft(word) ' strip leading punctuation
word =
trimPunctuationRight(word) ' strip trailing punctuation
End Sub
'---------------------------------------------------------------------------------
' This routine returns the number of words in a string
by counting the number of
' blanks.
'---------------------------------------------------------------------------------
Private Function countWords(ByVal line As String) As
Integer
Dim blankPosition As
Integer, beginning As Integer, wordCtr As Integer
wordCtr = 0
beginning = 0
blankPosition = 0
While blankPosition >= 0
' skip any leading or embedded spaces
While line.Substring(beginning, 1) = " "
beginning = beginning + 1
End While
' find the position of the next blank
blankPosition = line.IndexOf(" ", beginning)
'MsgBox("Blank found in position " & blankPosition)
wordCtr = wordCtr + 1
beginning = blankPosition + 1
End While
Return wordCtr
End Function
'---------------------------------------------------------------------------------
' Remove leading punctuation
'---------------------------------------------------------------------------------
Private Function trimPunctuationLeft(ByVal word As
String) As String
While Not
isAlphaCharacter(firstChar(word))
word = removeFirstChar(word) ' extract all characters to right of first
character
End While
Return word
End Function
'---------------------------------------------------------------------------------
' Remove trailing punctuation
'---------------------------------------------------------------------------------
Private Function trimPunctuationRight(ByVal word As
String) As String
While Not
isAlphaCharacter(lastChar(word))
word = removeLastChar(word)
End While
Return word
End Function
'---------------------------------------------------------------------------------
' returns a specified number of characters from the
left-hand side of a String.
'---------------------------------------------------------------------------------
Public Function leftStr(ByVal line As String, ByVal
numchars As Integer) As String
Return line.Substring(0,
numchars)
End Function
'---------------------------------------------------------------------------------
' returns a specified number of characters from the
right-hand side of a String.
'---------------------------------------------------------------------------------
Public Function rightStr(ByVal line As String, ByVal
numchars As Integer) As String
Return
line.Substring(line.Length - numchars)
End Function
'---------------------------------------------------------------------------------
' returns the first (leftmost) character in a string
'---------------------------------------------------------------------------------
Public Function firstChar(ByVal line As String) As
String
Return line.Substring(0,
1)
End Function
'---------------------------------------------------------------------------------
' returns the last (rightmost) character in a string
'---------------------------------------------------------------------------------
Public Function lastChar(ByVal line As String) As
String
Return
line.Substring(line.Length - 1)
End Function
'---------------------------------------------------------------------------------
' returns true is parameter is uppercase or lowercase
character
'---------------------------------------------------------------------------------
Public Function isAlphaCharacter(ByVal ch As String) As
Boolean
Return Asc(ch) >= 65 And
Asc(ch) <= 90 Or _
Asc(ch) >= 97 And Asc(ch) <= 122
End Function
'---------------------------------------------------------------------------------
' extract all characters to right of first character
'---------------------------------------------------------------------------------
Public Function removeFirstChar(ByVal line As String)
As String
Return line.Substring(1)
End Function
'---------------------------------------------------------------------------------
' extract all characters up to and excluding the last
one
'---------------------------------------------------------------------------------
Public Function removeLastChar(ByVal line As String) As
String
Return line.Substring(0,
line.Length - 1)
End Function
End Class
sln
file |