www.delorie.com/gnu/docs/elisp-manual-21/elisp_74.html   search  
Buy the book!

GNU Emacs Lisp Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.6 Conversion of Characters and Strings

This section describes functions for conversions between characters, strings and integers. format and prin1-to-string (see section 19.5 Output Functions) can also convert Lisp objects into strings. read-from-string (see section 19.3 Input Functions) can "convert" a string representation of a Lisp object into an object. The functions string-make-multibyte and string-make-unibyte convert the text representation of a string (see section 33.2 Converting Text Representations).

See section 24. Documentation, for functions that produce textual descriptions of text characters and general input events (single-key-description and text-char-description). These functions are used primarily for making help messages.

Function: char-to-string character
This function returns a new string containing one character, character. This function is semi-obsolete because the function string is more general. See section 4.3 Creating Strings.

Function: string-to-char string
This function returns the first character in string. If the string is empty, the function returns 0. The value is also 0 when the first character of string is the null character, ASCII code 0.

(string-to-char "ABC")
     => 65
(string-to-char "xyz")
     => 120
(string-to-char "")
     => 0
(string-to-char "\000")
     => 0

This function may be eliminated in the future if it does not seem useful enough to retain.

Function: number-to-string number
This function returns a string consisting of the printed base-ten representation of number, which may be an integer or a floating point number. The returned value starts with a minus sign if the argument is negative.

(number-to-string 256)
     => "256"
(number-to-string -23)
     => "-23"
(number-to-string -23.5)
     => "-23.5"

int-to-string is a semi-obsolete alias for this function.

See also the function format in 4.7 Formatting Strings.

Function: string-to-number string &optional base
This function returns the numeric value of the characters in string. If base is non-nil, integers are converted in that base. If base is nil, then base ten is used. Floating point conversion always uses base ten; we have not implemented other radices for floating point numbers, because that would be much more work and does not seem useful. If string looks like an integer but its value is too large to fit into a Lisp integer, string-to-number returns a floating point result.

The parsing skips spaces and tabs at the beginning of string, then reads as much of string as it can interpret as a number. (On some systems it ignores other whitespace at the beginning, not just spaces and tabs.) If the first character after the ignored whitespace is neither a digit, nor a plus or minus sign, nor the leading dot of a floating point number, this function returns 0.

(string-to-number "256")
     => 256
(string-to-number "25 is a perfect square.")
     => 25
(string-to-number "X256")
     => 0
(string-to-number "-4.5")
     => -4.5
(string-to-number "1e5")
     => 100000.0

string-to-int is an obsolete alias for this function.

Here are some other functions that can convert to or from a string:

concat can convert a vector or a list into a string. See section 4.3 Creating Strings.

vconcat can convert a string into a vector. See section 6.5 Functions for Vectors.

append can convert a string into a list. See section 5.5 Building Cons Cells and Lists.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003