Help please: String analysis

Hello,

I have several strings, sentences, of varying lengths.
I would like to make five strings from each of those strings as follows:

Lets take “The quick brown fox jumps over the lazy dog” as a working example.

Dim MyString, theLastWord as String
MyString = “The quick brown fox jumps over the lazy dog”

the first word… The… = NthField(MyString, " ", 1)
the second word… quick = NthField(MyString, " ", 2)
the third word… brown = NthField(MyString, " ", 3)

the last word… dog
Dim theLastWordInteger as integer
theLastWordInteger = CountFields(MyString, " ")
theLastWord = NthField(MyString, " ", theLastWordInteger)

msgbox NthField(MyString, " ", 1) + EndOfLine.Macintosh + NthField(MyString, " ", 2) + EndOfLine.Macintosh + NthField(MyString, " ", 3) + EndOfLine.Macintosh + theLastWord

the remainder of the first string… fox jumps over the lazy

How do I code for the remainder of the first string?

Thanks.

Lennox

Hi @Lennox_Jacob1,

you could simplify your code:

Dim MyString As String
MyString = "The quick brown fox jumps over the lazy dog"

Dim words() As String = MyString.Split

MsgBox(Join(words, EndOfline))

Now you can read/write to the words Array via index.

Thanks Martin,

That would put each word in a different line.

My request is to put the first three words in three separate lines, then the last word in a fourth line and finally the remainder of the string in a fifth line.

How can I put the remainder of the string in a fifth line?

Thanks,

Lennox

@Martin_T gave you the starting point. Once you have an array, you can pick out the words you want from any position. Then you can delete those elements from the array and re-join it into the part you want.

var words() as string = MyString.Split( " " )
var firstWord as string = words( 0 )
var secondWord as string = words( 1 )
var thirdWord as string = words( 2 )
var lastWord as string = words( words.LastIndex )

// Remove those words
call words.Pop // Easy way to get rid of the last word
for index as integer = 2 downto 0
  words.RemoveAt( index )
next

var restOfSentence as string = String.FromArray( words, " " )

Or something like that.

Note that this does not include any kind of error checking. If the sentence if short, for example, you will get an OutOfBoundsException, and if there is more than one space between words, you will get blank entries in the array.

2 Likes

An alternate way to do this is with a regular expression.

Off the of my head (untested):

var rx as new RegEx
rx.SearchPattern = "^(\w+) +(\w+) +(\w+) *(\b.*\b) +(\w+)$"
var match as RegExMatch = rx.Search( MyString )

var firstWord as string = match.SubExpressionString( 1 )
var secondWord as string = match.SubExpressionString( 2 )
var thirdWord as string = match.SubExpressionString( 3 )
var restOfSentence as string = match.SubExpressionString( 4 )
var lastWord as string = match.SubExpressionString( 5 )

This has the advantage of not caring about how many spaces there are or if the sentence is only four words, but assumes no punctuation.

Thanks Kem,

I was just thinking along those lines and that should do it.

Thanks again.

Lennox

Thanks Martin… for a good start.
Thanks Kim… very informative and works great.

Thanks again.
Lennox

1 Like