9.77 StringΒΆ

The type String provides character strings. Character strings provide all the operations for a one-dimensional array of characters, plus additional operations for manipulating text. For more information on related topics, see CharacterXmpPage and CharacterClassXmpPage . You can also issue the system command )show String to display the full list of operations defined by String.

String values can be created using double quotes.

hello := "Hello AXIOM!"
\[\]
“HelloAXIOM!”

Type: String

Note, however, that double quotes and underscores must be preceded by an extra underscore.

said := "Jane said, _"Look!_""
\[\]
“Janesaid,”Look!”“

Type: String

saw := "She saw exactly one underscore: __."
\[\]

Type: String

It is also possible to use newnewString to create a string of any size filled with a given character. Since there are many new functions it is necessary to indicate the desired type.

gasp: String := new(32, char "x")
\[\]
“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”

Type: String

The length of a string is given by #.

#gasp
\[\]
32

Type: PositiveInteger

Indexing operations allow characters to be extracted or replaced in strings. For any string s, indices lie in the range 1.. #s.

hello.2
\[\]
e

Type: Character

Indexing is really just the application of a string to a subscript, so any application syntax works.

hello 2
\[\]
e

Type: Character

hello(2)
\[\]
e

Type: Character

If it is important not to modify a given string, it should be copied before any updating operations are used.

hullo := copy hello
\[\]
“Hello,I’mAXIOM!”

Type: String

hullo.2 := char "u"; [hello, hullo]
\[\]
[“Hello,I’mAXIOM!”,”Hullo,I’mAXIOM!”]

Type: List String

Operations are provided to split and join strings. The concatconcatString operation allows several strings to be joined together.

saidsaw := concat ["alpha","---","omega"]
\[\]
“alpha—omega”

Type: String

There is a version of concatconcatString that works with two strings.

concat("hello ","goodbye")
\[\]
“hellogoodbye”

Type: String

Juxtaposition can also be used to concatenate strings.

"This " "is " "several " "strings " "concatenated."
\[\]
“Thisisseveralstringsconcatenated.”

Type: String

Substrings are obtained by giving an index range.

hello(1..5)
\[\]
“Hello”

Type: String

hello(8..)
\[\]
“I’mAXIOM!”

Type: String

A string can be split into several substrings by giving a separation character or character class.

split(hello, char " ")
\[\]
[“Hello,”,”I’m”,”AXIOM!”]

Type: List String

other := complement alphanumeric();

Type: CharacterClass

split(saidsaw, other)
\[\]
[“alpha”,”omega”]

Type: List String

Unwanted characters can be trimmed from the beginning or end of a string using the operations trimtrimString, leftTrimleftTrimString and rightTrimrightTrimString.

trim(" # # ++ relax ++ # #", char " #")
\[\]
“++relax++”

Type: String

Each of these functions takes a string and a second argument to specify the characters to be discarded.

trim(" # # ++ relax ++ # #", other)
\[\]
“relax”

Type: String

The second argument can be given either as a single character or as a character class.

leftTrim (" # # ++ relax ++ # #", other)
\[\]
“relax++ # #”

Type: String

rightTrim(" # # ++ relax ++ # #", other)
\[\]
” # #++relax”

Type: String

Strings can be changed to upper case or lower case using the operations upperCaseupperCaseString, upperCase!upperCase!String, lowerCaselowerCaseString and lowerCase!lowerCase!String.

upperCase hello
\[\]
“HELLO,I’MAXIOM!”

Type: String

The versions with the exclamation mark change the original string, while the others produce a copy.

lowerCase hello
\[\]
“hello,i’maxiom!”

Type: String

Some basic string matching is provided. The function prefix?prefix?String tests whether one string is an initial prefix of another.

prefix?("He", "Hello")
\[\]
true

Type: Boolean

prefix?("Her", "Hello")
\[\]
false

Type: Boolean

A similar function, suffix?suffix?String, tests for suffixes.

suffix?("", "Hello")
\[\]
true

Type: Boolean

suffix?("LO", "Hello")
\[\]
false

Type: Boolean

The function substring?substring?String tests for a substring given a starting position.

substring?("ll", "Hello", 3)
\[\]
true

Type: Boolean

substring?("ll", "Hello", 4)
\[\]
false

Type: Boolean

A number of positionpositionString functions locate things in strings. If the first argument to position is a string, then position(s,t,i) finds the location of s as a substring of t starting the search at position i.

n := position("nd", "underground", 1)
\[\]
2

Type: PositiveInteger

n := position("nd", "underground", n+1)
\[\]
10

Type: PositiveInteger

If s is not found, then 0 is returned (minIndex(s)-1 in IndexedString).

n := position("nd", "underground", n+1)
\[\]
0

Type: NonNegativeInteger

To search for a specific character or a member of a character class, a different first argument is used.

position(char "d", "underground", 1)
\[\]
3

Type: PositiveInteger

position(hexDigit(), "underground", 1)
\[\]
3

Type: PositiveInteger