Checking second-last word of a sentence?

U

unba1300

Guest
Could someone please tell me how to check if the second-last word in a sentence is a specific word? I'm guessing LastIndexOf2 somehow?

In other words, this is what I'd like to do,
If second-last word of sentence = "their" then
add an 's' to the following word if it doesn't already end with 's'
 
Last edited by a moderator:

NJDude

Expert
Licensed User
Longtime User
This will give you an idea:
B4X:
Dim Sentence As String
Dim Words() As String
            
Sentence = "First second third fourh"
            
Words = Regex.Split(" ", Sentence)
            
Msgbox("Words found= " & Words.Length & CRLF &" and the one before last is= " & Words(Words.Length - 2), "")

To do the "s" thing you will have to use "EndsWith"
 
Upvote 0
U

unba1300

Guest
Thank you, NJDude.
I got it working with this...
B4X:
Dim Words() As String
Words = Regex.Split(" ", sent)
If Words.Length > 1 Then
  If Words(Words.Length - 2) = "their" Then
    If Words(Words.Length - 1).EndsWith("ey") Then 
      sent= sent.SubString2(0, sent.Length -2) & "ies"
    Else If Words(Words.Length - 1).EndsWith("ny") Then
      sent= sent.SubString2(0, sent.Length -1) & "ies"
    Else If Words(Words.Length - 1).EndsWith("es") OR _ 
      Words(Words.Length - 1).EndsWith("th") Then
      'don't change the ending.
    Else If Words(Words.Length - 1).EndsWith("s") Then
      sent = sent & "es"
    Else
      sent = sent & "s"
    End If
  End If
End If
But now I realize my logic was wrong. I need to make the word plural following the word 'their' no matter where the word 'their' is in the sentence. Time for a break.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
In that case add a loop to search for the word "their"
B4X:
Dim ToPluralize As String
...

For I = 0 to Words.Lenght - 1

    If Words(I) = "their" Then

       ToPluralize = Words(I + 1)

    End If

Next

Of course the above code is a crude attempt, but maybe it will help you visualize your solution better.
 
Upvote 0
Top