Next: , Previous: , Up: Sequences   [Contents][Index]


5.5.4 Strings

A string is a vector of characters (see Characters). It is generally used for storing and manipulating pieces of text. Librep puts no restrictions on the values which may be stored in a string—specifically, the null character (‘^@’) may be stored with no problems.

In librep, a string is a sequcene of raw bytes, and no encoding is assumed. Some special functions for utf-8 encoded strings are provided. See See utf-8.

The read syntax of a string is a double quote character, followed by the contents of the string, the object is terminated by a second double quote character. For example, "abc" is the read syntax of the string ‘abc’.

Any backslash characters in the string’s read syntax introduce an escape sequence; one or more of the following characters are treated specially to produce the next actual character in the string.

The following escape sequences are supported (all are shown without their leading backslash ‘\’ character).

n

A newline character.

r

A carriage return character.

f

A form feed character.

t

A TAB character.

a

A ‘bell’ character (this is Ctrl-g).

\

A backslash character.

^c

The ‘control’ code of the character c. This is calculated by toggling the seventh bit of the upper-case version of c.

For example,

\^C             ;A Ctrl-c character (ASCII value 3)
\^@            ;The NUL character (ASCII value 0)
012

The character whose ASCII value is the octal value ‘012’. After the backslash character the Lisp reader reads up to three octal digits and combines them into one character.

x12

The character whose ASCII value is the hexadecimal value ‘12’, i.e. an ‘x’ character followed by one or two hex digits.

Others

If none of the above applies, the leading backslash is ignored. This is sometimes useful for editors syntax parsing, especially for punctuation characters.

Function: stringp object

This function returns true if its argument is a string.

Function: make-string length #!optional initial-character

Creates a new string containing length characters, each character is initialised to initial-character (or to spaces if initial-character is not defined).

(make-string 3)
    ⇒ "   "

(make-string 2 ?$)
    ⇒ "$$"
Function: concat #!rest args

This function concatenates all of its arguments, args, into a single string which is returned. If no arguments are given then the null string (‘’) results.

Each of the args may be a string, a character or a list or vector of characters. Characters are stored in strings modulo 256.

(concat "foo" "bar")
    ⇒ "foobar"

(concat "a" ?b)
    ⇒ "ab"

(concat "foo" [?b ?a ?r])
    ⇒ "foobar"

(concat)
    ⇒ ""
Function: substring string start #!optional end

This function creates a new string which is a partial copy of the string string. The first character copied is start characters from the beginning of the string. If the end argument is defined it is the index of the character to stop copying at, if it is not defined all characters until the end of the string are copied.

(substring "xxyfoozwx" 3 6)
    ⇒ "foo"

(substring "xyzfoobar" 3)
    ⇒ "foobar"

For utf-8 encoded strings, use utf8-substring instead (see utf-8).

Function: string= string1 string2

This function compares the two strings string1 and string2—if they are made from the same characters in the same order then true is returned.

(string= "one" "one")
    ⇒ t

(string= "one" "two")
    ⇒ ()

Note that an alternate way to compare strings (or anything!) is to use the equal function.

Function: string-equal string1 string2

Returns true if string1 and string2 are the same, ignoring differences in character case.

Function: string< string1 string2

This function returns true if string1 is ‘less’ than string2. This is determined by comparing the two strings a character at a time, the first pair of characters which do not match each other are then compared with a normal ‘less-than’ function.

In Librep the standard < function understands strings so string< is just a macro calling that function.

(string< "abc" "abd")
    ⇒ t

(string< "abc" "abb")
    ⇒ ()
Function: string-lessp string1 string2

Similar to string< but ignores character case in comparisons.

See String Functions for a few more string manipulating functions, and Regular Expressions for a method of pattern matching in strings.


Next: , Previous: , Up: Sequences   [Contents][Index]