]>
Book Contents Previous Section 9.76 Stream Next Section 9.78 StringTable Book Index

## 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: __."

"Shesawexactlyoneunderscore:_."

Type: String

It is also possible to use new new String 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 concat concat String operation allows several strings to be joined together.

saidsaw := concat ["alpha","---","omega"]

"alpha---omega"

Type: String

There is a version of concat concat String 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 trim trim String, leftTrim leftTrim String and rightTrim rightTrim String.

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 upperCase upperCase String, upperCase!upperCase!String, lowerCase lowerCase String 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 position position String 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
Book Contents Previous Section 9.76 Stream Next Section 9.78 StringTable Book Index