How do I identify the longest word in a string?

0 favourites
  • 5 posts
From the Asset Store
Word Guess
$10 USD
Questions and answers are stored in simple txt-files. Cool animation of everything
  • How do I identify the longest word or "group" of characters/textwidth in a string?

    For instance: Was your day better today or yesterday? The longest word would be "yesterday" = 9

    OR: Is there traffic in downtown? The longest word would be "downtown". = 8

    I don't need to know the word just how many "characters" so that I can determine width.

  • System expression len('text') returns the number of characters

  • Thanks for your suggestion.

    When I do this it gives a number for the whole string.

    For instance: Was your day better today or yesterday? I get 39 because it is including the whole string.

    I just want the identify the longest word in the string. In this example "yesterday" should come out to 9.

  • You can use another system expression tokenat("text", index, separator) to do this. So with this expression you can call each word of the sentence in turn which will have index 0, 1, 2 etc, then once returned you can use len("returned text") to calculate the length of that returned word. Then you will need logic to find the largest of the returned lengths, so you could store the word and length, then compare back against it, if the length is greater then that becomes the latest word.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Works:) Thank you so much!!!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)