(lispref.info)String Conversion


Next: Formatting Strings Prev: Text Comparison Up: Strings and Characters

Conversion of Characters and Strings
====================================

   Characters and strings may be converted into each other and into
integers.  `format' and `prin1-to-string' (Note: Output Functions.)
may also be used to convert Lisp objects into strings.
`read-from-string' (Note: Input Functions.) may be used to "convert"
a string representation of a Lisp object into an object.

   Note: Documentation, for a description of functions which return a
string representing the Emacs standard notation of the argument
character (`single-key-description' and `text-char-description').
These functions are used primarily for printing help messages.

 - Function: char-to-string CHARACTER
     This function returns a new string with a length of one character.
     The value of CHARACTER, modulo 256, is used to initialize the
     element of the string.

     This function is similar to `make-string' with an integer argument
     of 1.  (Note: Creating Strings.)  This conversion can also be
     done with `format' using the `%c' format specification.  (*Note
     Formatting Strings::.)

          (char-to-string ?x)
               => "x"
          (char-to-string (+ 256 ?x))
               => "x"
          (make-string 1 ?x)
               => "x"

 - 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
 - Function: int-to-string NUMBER
     This function returns a string consisting of the printed
     representation of NUMBER, which may be an integer or a floating
     point number.  The value starts with a sign if the argument is
     negative.

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

     See also the function `format' in Note: Formatting Strings.

 - Function: string-to-number STRING
 - Function: string-to-int STRING
     This function returns the integer value of the characters in
     STRING, read as a number in base ten.  It skips spaces 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.)  If the first
     character after the ignored whitespace is not a digit or a minus
     sign, 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


automatically generated by info2www