String and text manipulation
This chapter is more a "how-to" than actual Red studying. Any suggestions for improvements are welcome.
split
Creates a block (a series) containing the pieces of a string that are separated by a delimiter. This delimiter is given as an argument. split
is particularly useful to analyse and manipulate text files.
>> s: "My house is a very funny house" ;some console lines ahead removed for clarity
>> split s " " ;every space is a delimiter.
== ["My" "house" "is" "a" "very" "funny" "" "" "" "" "house"] ;result is a series with 11 elements.
>> s: "My house ; is a very ; funny house"
>> split s ";" ;split happens at the semi-colons.
== ["My house " " is a very " " funny house"] ;result is a series with 3 elements.
removing characters: trim
word trim
with no refinements removes white space (tabs and spaces) from the beginning and end of a string! (it also removes none
from a block! or object!). The value of the argument is altered. It has a refinement to remove specific characters.
Refinements:
/head - Removes only from the head.
/tail - Removes only from the tail.
/auto - Auto indents lines relative to first line.
/lines - Removes all line breaks and extra spaces.
/all - Removes all whitespace (but not line breaks).
/with - Same as /all, but removes characters in a 'with' argument we supply. It must be one of: char! string! integer!
>> e: " spaces before and after " ; some console lines ahead removed for clarity
>> trim e
== "spaces before and after"
trim leading spaces
>> e: " spaces before and after " ; some console lines ahead removed for clarity
>> trim/head e
== "spaces before and after "
trim trailing spaces
>> e: " spaces before and after " ; some console lines ahead removed for clarity
>> trim/tail e
== " spaces before and after"
trim specific characters
>> d: "our house in the middle of our street" ; some console lines ahead removed for clarity
>> trim/with d " "
== "ourhouseinthemiddleofourstreet"
>> a: "house" ; some console lines ahead removed for clarity
>> trim/with a "u"
== "hose"
the opposite of trim: pad
pad
expands the string to a given size by adding spaces. The default is to add spaces to the right, but with the refinement /left
, spaces are added to the beginning of the string. Changes the original string, beware.
>> a: "House" ; some console lines ahead removed for clarity
>> pad a 10
== "House "
>> pad/left a 20
== " House "
concatenation: rejoin
>> a: "house" b: " " c: "entrance" ; some console lines ahead removed for clarity
>> rejoin [a b c]
== "house entrance"
or, using append - this changes the original series
>> append a b
== "housemanipulation"
>> a: "house" b: " " c: "entrance" ; some console lines ahead removed for clarity
>> append a c
== "houseentrance"
>> append a append b c
== "houseentrance entrance" ; "a" was changed to "houseentrance" in the last manipulation
turning a series into text: form
form
turns a series into a string, removing brackets and adding spaces between elements. form
was briefly seen in the Other useful things chapter.
>> a: ["my" "house" 23 47 4 + 8 ["a" "bee" "cee"]] ;some console lines ahead removed for clarity
>> form a
== "my house 23 47 4 + 8 a bee cee"
/part
The refinement /part
limits the number of characters of the created string.
>> a: ["my" "house" 23 47 4 + 8 ["a" "bee" "cee"]] ;some console lines ahead removed for clarity
>> form/part a 8
== "my house"
string length
>> f: "my house" ;some console lines ahead removed for clarity
>> length? f ; see chapter "Series 'getters'"
== 8
left part of string
using copy/part
>> s: "nasty thing" ;some console lines ahead removed for clarity
>> b: copy/part s 5
== "nasty"
right part of string
using at
>> s: "nasty thing" ;some console lines ahead removed for clarity
>> at tail s -5
== "thing"
using remove/part
- this changes the original series, beware!
>> s: "nasty thing" ; some console lines ahead removed for clarity
>> remove/part s 6
== "thing"
middle part of string
using copy/part
and at
:
>> a: "abcdefghijkl" ; some console lines ahead removed for clarity
>> copy/part at a 4 3
== "def"
insert strings
at the beginning, using insert
:
>> s: "house" ; some console lines ahead removed for clarity
>> insert s "beautiful "
>> s
== "beautiful house"
at the end, using append
:
>> s: "beautiful" ; some console lines ahead removed for clarity
>> append s " day"
== "beautiful day"
in the middle, using insert at
:
>> s: "nasty thing" ; some console lines ahead removed for clarity
>> insert at s 7 "little "
>> s
== "nasty little thing"
lowercase
Changes the original string, beware.
>> a: "mY HoUse" ; some console lines ahead removed for clarity
>> lowercase a
== "my house"
/part
>> a: "mY HoUse" ; some console lines ahead removed for clarity
>> lowercase/part a 2
== "my HoUse"
uppercase
>> a: "mY HoUse" ; some console lines ahead removed for clarity
>> uppercase a
== "MY HOUSE"
/part
>> a: "mY HoUse" ; some console lines ahead removed for clarity
>> uppercase/part a 2
== "MY HoUse"