needmoreeasy EN 한국어
Learn

51 — Text: counting, splitting, joining again

★★★☆☆ (3/5)lists and text
Prerequisites
05 — Set, 40 — CSV
You will end up with
counting the letters in a sentence, splitting it into words, and joining it back into one line

Text is what programs handle most. What a person types is text and what comes out of a file is text. There are three things to do with it — count it, split it, join it.

Steps

  1. The number of letters is its length. Spaces count too:

    set line to the weather is very good today
    show the length of line
    
    run it →

    You get 30.

  2. Splitting by space gives a list of words. It is the same sentence as splitting by comma in guide 40:

    set line to the weather is very good today
    set words to line split by space
    show how many words
    show the first of words
    show the last of words
    
    run it →

    6, the, today. What comes out is a list, so every list sentence works on it straight away.

  3. It joins back into one line too:

    set words to list of the, weather, is, good
    show words joined by comma
    show words joined by space
    
    run it →

    the, weather, is, good and the weather is good.

  4. One star per word is a way to see the length. Put the count in a name first:

    set words to list of the, weather, is, good
    set count to how many words
    set star to *
    set bar to star repeated count times
    show bar
    
    run it →

    You get ****.

  5. Letters can be turned into capitals or small letters:

    set line to the weather is very good today
    show line in capitals
    show line in small letters
    
    run it →

    THE WEATHER IS VERY GOOD TODAY, then the line as it was.

  6. The whole thing:

    set line to the weather is very good today
    show the length of line
    set words to line split by space
    show how many words
    show the first of words
    show the last of words
    show words joined by comma
    set count to how many words
    set star to *
    set bar to star repeated count times
    show bar
    
    run it →

Try it yourself

Split by comma instead of by space — there are no commas in the line, so the whole thing comes back as one piece. What you split on has to actually be in the text. Then sort the words you split out and you have them alphabetically.

What you learned

  • the length of <text> is how many letters it has, spaces included.
  • <text> split by space makes a list of words, and it is a list like any other.
  • <list> joined by <separator> puts it back into one line.
  • in capitals and in small letters change letters that have two cases.

This page on GitHub