Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

Accessing parts of text in text boxes (70pts)

How can I read a specific word of a certain line in a text box? If I have a box containing lots of words, I want to be able to get a certain word and read it by referring to some kind of address, e.g.

If I had a box containing the sentence "The cat sat on the mat.", how could I ask the program to return, say, word 3?
0
The_Oracle
Asked:
The_Oracle
1 Solution
 
lankfordCommented:
As long as you are using a vb text box, you are probably going to have to parse the text and figure it out your self.

If you are programmatically using Microsoft Word as your text box, then you can actually refer to the third word in the Word object with something like this:

   ThirdWord = Document.Word(3)

Does anyone out there know of a text box ocx control that stores its information in easily accessible objects like this?

Lankford

0
 
OeilNoirCommented:
Yep, you would have to check all text, but you could write a function who would create a kind of index of words. something what would keep the position of the word and it's lenth
then with the index you could get your word

"this is my string and i want to find a word in it"

for the word "my" you would keep the position (9) and the lenth (2) . and you could read your index to know where the words are. would be longer whe you create your index, but faster when you search your word...
0
 
MikeP090797Commented:
Here are some parsing functions you might find useful to achive that task:

'PURPOSE:    Finds part of a string found after the N occurence of a substring
'ARGUMENTS:
'   IN szMainString:    string to extract from
'   IN szSubString:     substring to look for
'   IN iOccurence:      the number of the occurence to return the string after it
'   IN bCaseSensetive:  specifies whether the search is case sensetive
'RETURNS:    The string located after the first occurence of a substring
'
Public Function GetAfterSubstring(szMainString As String, szSubString As String, Optional iOccurence As Integer = 1, Optional bCaseSensetive As Boolean = False) As String
    Dim iPos As Integer
    iPos = InstrN(szMainString, szSubString, iOccurence, bCaseSensetive)
   
    'Return "" if substring is not found, or if there is nothing after it
    If iPos = 0 Or iPos = Len(szMainString) Then
        GetAfterSubstring = ""
        Exit Function
    End If
   
    GetAfterSubstring = Right(szMainString, Len(szMainString) - iPos)
End Function
'PURPOSE:    Finds part of a string found before the N occurence of a substring
'ARGUMENTS:
'   IN szMainString:    string to extract from
'   IN szSubString:     substring to look for
'   IN iOccurence:      the number of the occurence to return the string before it
'   IN bCaseSensetive:  specifies whether the search is case sensetive
'RETURNS:    The string located before the first occurence of a substring
'
Public Function GetBeforeSubstring(szMainString As String, szSubString As String, Optional iOccurence As Integer = 1, Optional bCaseSensetive As Boolean = False) As String
    Dim iPos As Integer
    iPos = InstrN(szMainString, szSubString, iOccurence, bCaseSensetive)
   
    'Return "" if substring is not found
    If iPos = 0 Then
        GetBeforeSubstring = ""
        Exit Function
    End If
   
    GetBeforeSubstring = Left(szMainString, iPos - 1)
End Function
'PURPOSE:   Locates the position of the last occurence of a substring in a string
'ARGUMENTS:
'   IN szMainString:    string to search
'   IN szSubString:     substring to look for
'   IN bCaseSensetive   Specifies wheteher the string comparison method is case sensetive
'RETURNS:   The position of the last substring in a string
'
Public Function InstrL(szMainString As String, szSubString As String, Optional bCaseSensetive As Boolean = False) As Integer
    Dim I As Integer
    Dim nLen As Integer
    Dim nLen1 As Integer
    Dim iComparisonMethod As Integer
    Dim bFlag As Boolean
   
    nLen = Len(szMainString)
    nLen1 = Len(szSubString)
    If nLen = 0 Or nLen1 = 0 Then Exit Function
   
    'Set comparison method
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)
   
    'Look for the first substring from the end
    For I = nLen - nLen1 To 1 Step -1
        If InStr(I, szMainString, szSubString, iComparisonMethod) > 0 Then
            bFlag = True
            Exit For
        End If
    Next I
   
    If Not bFlag Then
        InstrL = 0
        Exit Function
    End If
   
    InstrL = I
End Function
'PURPOSE:   Locates the position of the N occurence of a substring in a string
'ARGUMENTS:
'   IN szMainString:    string to search
'   IN szSubString:     substring to look for
'   IN iOccurrance:     the number of the occurence of the substring to return
'   IN bCaseSensetive   Specifies wheteher the string comparison method is case sensetive
'RETURNS:   The position of the N substring in a string
'
Public Function InstrN(szMainString As String, szSubString As String, Optional iOccurance As Integer = 1, Optional bCaseSensetive As Boolean = False) As Integer
    Dim I As Integer
    Dim iComparisonMethod As Integer
    Dim iPos As Integer
   
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)
    iPos = 1
   
    If iOccurance = 1 Then
        InstrN = InStr(1, szMainString, szSubString, iComparisonMethod)
        Exit Function
    End If
   
   
    For I = 1 To iOccurance 'Look for the substring N times
        iPos = InStr(iPos + 1, szMainString, szSubString, iComparisonMethod)
        If iPos = 0 Then    'If no more found, means that there are less occurences then specified
            InstrN = 0
            Exit Function
        End If
    Next I
   
    InstrN = iPos
End Function

'PURPOSE:   Counts how many times a substring appears in a string
'ARGUMENTS:
'   IN szMainString:    string to look in
'   IN szSubString:     string to look for
'   IN bCaseSensetive:  Specifies whether the comparison is case sensetive
'RETURNS:   The number of times the substring occures in the string
'
Public Function GetOccurrenceCount(szMainString As String, szSubString As String, Optional bCaseSensetive As Boolean = False) As Long
    Dim iPos As Integer
    Dim iCount As Integer
    Dim iComparisonMethod As Integer
   
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)   'Set comparison method
    iPos = InStr(1, szMainString, szSubString, iComparisonMethod)
   
    While iPos > 0      'Loop while finding substring
        iCount = iCount + 1
        iPos = InStr(iPos + 1, szMainString, szSubString, iComparisonMethod)
    Wend
   
    GetOccurrenceCount = iCount
End Function

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now