51 — Text: counting, splitting, joining again
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
The number of letters is its
length. Spaces count too:
run it →set line to the weather is very good today show the length of lineYou get
30.Splitting by space gives a list of words. It is the same sentence as splitting by comma in guide 40:
run it →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 words6,the,today. What comes out is a list, so every list sentence works on it straight away.It joins back into one line too:
run it →set words to list of the, weather, is, good show words joined by comma show words joined by spacethe, weather, is, goodandthe weather is good.One star per word is a way to see the length. Put the count in a name first:
run it →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 barYou get
****.Letters can be turned into capitals or small letters:
run it →set line to the weather is very good today show line in capitals show line in small lettersTHE WEATHER IS VERY GOOD TODAY, then the line as it was.The whole thing:
run it →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
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 spacemakes a list of words, and it is a list like any other.<list> joined by <separator>puts it back into one line.in capitalsandin small letterschange letters that have two cases.