String and text manipulation
Note: in the examples, some output lines of the console were removed for clarity.
function! split Red-by-example MyCode4fun
Returns a block (a series) containing the pieces of a string that are separated by a delimiter. Does not change original block. The delimiter is given as an argument. split is particularly useful to the parse dialect and to analyze and manipulate text files.
>> s: "My house is a very funny house"
>> 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: action! trim Red-by-example MyCode4fun
The 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. It returns the trimmed series and changes the original series.
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! or integer!
>> e: " spaces before and after "
>> trim e
== "spaces before and after"
trim leading spaces:
>> e: " spaces before and after "
>> trim/head e
== "spaces before and after "
trim trailing spaces:
>> e: " spaces before and after "
>> trim/tail e
== " spaces before and after"
trim specific characters:
>> d: "our house in the middle of our street"
>> trim/with d " "
== "ourhouseinthemiddleofourstreet"
>> a: "house"
>> trim/with a "u"
== "hose"
the opposite of trim: function! pad Red-by-example
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"
>> pad a 10
== "House "
>> pad/left a 20
== " House "
text concatenation: function! rejoin Red-by-example MyCode4fun
>> a: "house" b: " " c: "entrance"
>> rejoin [a b c]
== "house entrance"
or, using append - this changes the original series
>> append a c
== "house entrance"
>> a: "house" b: " " c: "entrance"
>> 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: action! form Red-by-example MyCode4fun
form returns a series as a string, removing brackets and adding spaces between elements. form was briefly seen in the Accessing and formating data chapter.
>> a: ["my" "house" 23 47 4 + 8 ["a" "bee" "cee"]]
>> 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"]]
>> form/part a 8
== "my house"
string length: action! length? Red-by-example MyCode4fun
>> f: "my house"
>> length? f
== 8
left part of string:
using copy/part :
>> s: "nasty thing"
>> b: copy/part s 5
== "nasty"
right part of string:
using at :
>> s: "nasty thing"
>> at tail s -5
== "thing"
using remove/part - this changes the original series, beware!
>> s: "nasty thing"
>> remove/part s 6
== "thing"
middle part of string:
using copy/part and at:
>> a: "abcdefghijkl"
>> copy/part at a 4 3
== "def"
insert strings:
at the beginning, using insert:
>> s: "house"
>> insert s "beautiful "
>> s
== "beautiful house"
at the end, using append:
>> s: "beautiful"
>> append s " day"
== "beautiful day"
in the middle, using insert at:
>> s: "nasty thing"
>> insert at s 7 "little "
>> s
== "nasty little thing"
native! lowercase Red-by-example MyCode4fun
Changes the original string, beware.
>> a: "mY HoUse"
>> lowercase a
== "my house"
/part
>> a: "mY HoUse"
>> lowercase/part a 2
== "my HoUse"
native! uppercase Red-by-example MyCode4fun
Changes the original string, beware.
>> a: "mY HoUse"
>> uppercase a
== "MY HOUSE"
/part
>> a: "mY HoUse"
>> uppercase/part a 2
== "MY HoUse"