Move inclusion of unistd.h to top, else fails on
[emacs.git] / lispref / strings.texi
blobb6ffa8ffee41eb46fc7fcbdfe325b57822d6456d
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. 
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/strings
6 @node Strings and Characters, Lists, Numbers, Top
7 @comment  node-name,  next,  previous,  up
8 @chapter Strings and Characters
9 @cindex strings
10 @cindex character arrays
11 @cindex characters
12 @cindex bytes
14   A string in Emacs Lisp is an array that contains an ordered sequence
15 of characters.  Strings are used as names of symbols, buffers, and
16 files, to send messages to users, to hold text being copied between
17 buffers, and for many other purposes.  Because strings are so important,
18 Emacs Lisp has many functions expressly for manipulating them.  Emacs
19 Lisp programs use strings more often than individual characters.
21   @xref{Strings of Events}, for special considerations for strings of
22 keyboard character events.
24 @menu
25 * Basics: String Basics.      Basic properties of strings and characters.
26 * Predicates for Strings::    Testing whether an object is a string or char.
27 * Creating Strings::          Functions to allocate new strings.
28 * Modifying Strings::         Altering the contents of an existing string.
29 * Text Comparison::           Comparing characters or strings.
30 * String Conversion::         Converting characters or strings and vice versa.
31 * Formatting Strings::        @code{format}: Emacs's analog of @code{printf}.
32 * Character Case::            Case conversion functions.
33 * Case Table::                Customizing case conversion.
34 @end menu
36 @node String Basics
37 @section String and Character Basics
39   Strings in Emacs Lisp are arrays that contain an ordered sequence of
40 characters.  Characters are represented in Emacs Lisp as integers;
41 whether an integer was intended as a character or not is determined only
42 by how it is used.  Thus, strings really contain integers.
44   The length of a string (like any array) is fixed, and cannot be
45 altered once the string exists.  Strings in Lisp are @emph{not}
46 terminated by a distinguished character code.  (By contrast, strings in
47 C are terminated by a character with @sc{ASCII} code 0.)
49   Since strings are considered arrays, you can operate on them with the
50 general array functions.  (@xref{Sequences Arrays Vectors}.)  For
51 example, you can access or change individual characters in a string
52 using the functions @code{aref} and @code{aset} (@pxref{Array
53 Functions}).
55   There are two text representations for non-@sc{ASCII} characters in
56 Emacs strings (and in buffers): unibyte and multibyte (@pxref{Text
57 Representations}).  @sc{ASCII} characters always occupy one byte in a
58 string; in fact, there is no real difference between the two
59 representation for a string which is all @sc{ASCII}.  For most Lisp
60 programming, you don't need to be concerned with these two
61 representations.
63   Sometimes key sequences are represented as strings.  When a string is
64 a key sequence, string elements in the range 128 to 255 represent meta
65 characters (which are extremely large integers) rather than keyboard
66 events in the range 128 to 255.
68   Strings cannot hold characters that have the hyper, super or alt
69 modifiers; they can hold @sc{ASCII} control characters, but no other
70 control characters.  They do not distinguish case in @sc{ASCII} control
71 characters.  If you want to store such characters in a sequence, such as
72 a key sequence, you must use a vector instead of a string.
73 @xref{Character Type}, for more information about representation of meta
74 and other modifiers for keyboard input characters.
76   Strings are useful for holding regular expressions.  You can also
77 match regular expressions against strings (@pxref{Regexp Search}).  The
78 functions @code{match-string} (@pxref{Simple Match Data}) and
79 @code{replace-match} (@pxref{Replacing Match}) are useful for
80 decomposing and modifying strings based on regular expression matching.
82   Like a buffer, a string can contain text properties for the characters
83 in it, as well as the characters themselves.  @xref{Text Properties}.
84 All the Lisp primitives that copy text from strings to buffers or other
85 strings also copy the properties of the characters being copied.
87   @xref{Text}, for information about functions that display strings or
88 copy them into buffers.  @xref{Character Type}, and @ref{String Type},
89 for information about the syntax of characters and strings.
90 @xref{Non-ASCII Characters}, for functions to convert between text
91 representations and encode and decode character codes.
93 @node Predicates for Strings
94 @section The Predicates for Strings
96 For more information about general sequence and array predicates,
97 see @ref{Sequences Arrays Vectors}, and @ref{Arrays}.
99 @defun stringp object
100   This function returns @code{t} if @var{object} is a string, @code{nil}
101 otherwise.
102 @end defun
104 @defun char-or-string-p object
105   This function returns @code{t} if @var{object} is a string or a
106 character (i.e., an integer), @code{nil} otherwise.
107 @end defun
109 @node Creating Strings
110 @section Creating Strings
112   The following functions create strings, either from scratch, or by
113 putting strings together, or by taking them apart.
115 @defun make-string count character
116   This function returns a string made up of @var{count} repetitions of
117 @var{character}.  If @var{count} is negative, an error is signaled.
119 @example
120 (make-string 5 ?x)
121      @result{} "xxxxx"
122 (make-string 0 ?x)
123      @result{} ""
124 @end example
126   Other functions to compare with this one include @code{char-to-string}
127 (@pxref{String Conversion}), @code{make-vector} (@pxref{Vectors}), and
128 @code{make-list} (@pxref{Building Lists}).
129 @end defun
131 @tindex string
132 @defun string &rest characters
133 This returns a string containing the characters @var{characters}.
135 @example
136 (string ?a ?b ?c)
137      @result{} "abc"
138 @end example
139 @end defun
141 @defun substring string start &optional end
142 This function returns a new string which consists of those characters
143 from @var{string} in the range from (and including) the character at the
144 index @var{start} up to (but excluding) the character at the index
145 @var{end}.  The first character is at index zero.
147 @example
148 @group
149 (substring "abcdefg" 0 3)
150      @result{} "abc"
151 @end group
152 @end example
154 @noindent
155 Here the index for @samp{a} is 0, the index for @samp{b} is 1, and the
156 index for @samp{c} is 2.  Thus, three letters, @samp{abc}, are copied
157 from the string @code{"abcdefg"}.  The index 3 marks the character
158 position up to which the substring is copied.  The character whose index
159 is 3 is actually the fourth character in the string.
161 A negative number counts from the end of the string, so that @minus{}1
162 signifies the index of the last character of the string.  For example: 
164 @example
165 @group
166 (substring "abcdefg" -3 -1)
167      @result{} "ef"
168 @end group
169 @end example
171 @noindent
172 In this example, the index for @samp{e} is @minus{}3, the index for
173 @samp{f} is @minus{}2, and the index for @samp{g} is @minus{}1.
174 Therefore, @samp{e} and @samp{f} are included, and @samp{g} is excluded.
176 When @code{nil} is used as an index, it stands for the length of the
177 string.  Thus,
179 @example
180 @group
181 (substring "abcdefg" -3 nil)
182      @result{} "efg"
183 @end group
184 @end example
186 Omitting the argument @var{end} is equivalent to specifying @code{nil}.
187 It follows that @code{(substring @var{string} 0)} returns a copy of all
188 of @var{string}.
190 @example
191 @group
192 (substring "abcdefg" 0)
193      @result{} "abcdefg"
194 @end group
195 @end example
197 @noindent
198 But we recommend @code{copy-sequence} for this purpose (@pxref{Sequence
199 Functions}).
201 If the characters copied from @var{string} have text properties, the
202 properties are copied into the new string also.  @xref{Text Properties}.
204 A @code{wrong-type-argument} error is signaled if either @var{start} or
205 @var{end} is not an integer or @code{nil}.  An @code{args-out-of-range}
206 error is signaled if @var{start} indicates a character following
207 @var{end}, or if either integer is out of range for @var{string}.
209 @code{substring} actually allows vectors as well as strings for
210 the first argument.
212 Contrast this function with @code{buffer-substring} (@pxref{Buffer
213 Contents}), which returns a string containing a portion of the text in
214 the current buffer.  The beginning of a string is at index 0, but the
215 beginning of a buffer is at index 1.
216 @end defun
218 @defun concat &rest sequences
219 @cindex copying strings
220 @cindex concatenating strings
221 This function returns a new string consisting of the characters in the
222 arguments passed to it (along with their text properties, if any).  The
223 arguments may be strings, lists of numbers, or vectors of numbers; they
224 are not themselves changed.  If @code{concat} receives no arguments, it
225 returns an empty string.
227 @example
228 (concat "abc" "-def")
229      @result{} "abc-def"
230 (concat "abc" (list 120 (+ 256 121)) [122])
231      @result{} "abcxyz"
232 ;; @r{@code{nil} is an empty sequence.}
233 (concat "abc" nil "-def")
234      @result{} "abc-def"
235 (concat "The " "quick brown " "fox.")
236      @result{} "The quick brown fox."
237 (concat)
238      @result{} ""
239 @end example
241 @noindent
242 The second example above shows how characters stored in strings are
243 taken modulo 256.  In other words, each character in the string is
244 stored in one byte.
246 The @code{concat} function always constructs a new string that is
247 not @code{eq} to any existing string.
249 When an argument is an integer (not a sequence of integers), it is
250 converted to a string of digits making up the decimal printed
251 representation of the integer.  @strong{Don't use this feature; we plan
252 to eliminate it.  If you already use this feature, change your programs
253 now!}  The proper way to convert an integer to a decimal number in this
254 way is with @code{format} (@pxref{Formatting Strings}) or
255 @code{number-to-string} (@pxref{String Conversion}).
257 @example
258 @group
259 (concat 137)
260      @result{} "137"
261 (concat 54 321)
262      @result{} "54321"
263 @end group
264 @end example
266 For information about other concatenation functions, see the
267 description of @code{mapconcat} in @ref{Mapping Functions},
268 @code{vconcat} in @ref{Vectors}, and @code{append} in @ref{Building
269 Lists}.
270 @end defun
272 @tindex split-string
273 @defun split-string string separators
274 Split @var{string} into substrings in between matches for the regular
275 expression @var{separators}.  Each match for @var{separators} defines a
276 splitting point; the substrings between the splitting points are made
277 into a list, which is the value.  If @var{separators} is @code{nil} (or
278 omitted), the default is @code{"[ \f\t\n\r\v]+"}.
280 For example,
282 @example
283 (split-string "Soup is good food" "o")
284 @result{} ("S" "up is g" "" "d f" "" "d")
285 (split-string "Soup is good food" "o+")
286 @result{} ("S" "up is g" "d f" "d")
287 @end example
289 When there is a match adjacent to the beginning or end of the string,
290 this does not cause a null string to appear at the beginning or end
291 of the list:
293 @example
294 (split-string "out to moo" "o+")
295 @result{} ("ut t" " m")
296 @end example
298 Empty matches do count, when not adjacent to another match:
300 @example
301 (split-string "Soup is good food" "o*")
302 @result{}("S" "u" "p" " " "i" "s" " " "g" "d" " " "f" "d")
303 (split-string "Nice doggy!" "")
304 @result{}("N" "i" "c" "e" " " "d" "o" "g" "g" "y" "!")
305 @end example
306 @end defun
308 @node Modifying Strings
309 @section Modifying Strings
311   The most basic way to alter the contents of an existing string is with
312 @code{aset} (@pxref{Array Functions}).  @code{(aset @var{string}
313 @var{idx} @var{char})} stores @var{char} into @var{string} at index
314 @var{idx}.  Each character occupies one or more bytes, and if @var{char}
315 needs a different number of bytes from the character already present at
316 that index, @code{aset} gets an error.
318   A more powerful function is @code{store-substring}:
320 @tindex store-substring
321 @defun store-substring string idx obj
322 This function alters part of the contents of the string @var{string}, by
323 storing @var{obj} starting at index @var{idx}.  The argument @var{obj}
324 may be either a character or a (smaller) string.
326 Since it is impossible to change the length of an existing string, it is
327 an error if @var{obj} doesn't fit within @var{string}'s actual length,
328 or if it requires a different number of bytes from the characters
329 currently present at that point in @var{string}.
330 @end defun
332 @need 2000
333 @node Text Comparison
334 @section Comparison of Characters and Strings
335 @cindex string equality
337 @defun char-equal character1 character2
338 This function returns @code{t} if the arguments represent the same
339 character, @code{nil} otherwise.  This function ignores differences
340 in case if @code{case-fold-search} is non-@code{nil}.
342 @example
343 (char-equal ?x ?x)
344      @result{} t
345 (let ((case-fold-search nil))
346   (char-equal ?x ?X))
347      @result{} nil
348 @end example
349 @end defun
351 @defun string= string1 string2
352 This function returns @code{t} if the characters of the two strings
353 match exactly; case is significant.
355 @example
356 (string= "abc" "abc")
357      @result{} t
358 (string= "abc" "ABC")
359      @result{} nil
360 (string= "ab" "ABC")
361      @result{} nil
362 @end example
364 The function @code{string=} ignores the text properties of the two
365 strings.  When @code{equal} (@pxref{Equality Predicates}) compares two
366 strings, it uses @code{string=}.
368 If the arguments contain non-@sc{ASCII} characters, and one is unibyte
369 while the other is multibyte, then they cannot be equal.  @xref{Text
370 Representations}.
371 @end defun
373 @defun string-equal string1 string2
374 @code{string-equal} is another name for @code{string=}.
375 @end defun
377 @cindex lexical comparison
378 @defun string< string1 string2
379 @c (findex string< causes problems for permuted index!!)
380 This function compares two strings a character at a time.  First it
381 scans both the strings at once to find the first pair of corresponding
382 characters that do not match.  If the lesser character of those two is
383 the character from @var{string1}, then @var{string1} is less, and this
384 function returns @code{t}.  If the lesser character is the one from
385 @var{string2}, then @var{string1} is greater, and this function returns
386 @code{nil}.  If the two strings match entirely, the value is @code{nil}.
388 Pairs of characters are compared by their @sc{ASCII} codes.  Keep in
389 mind that lower case letters have higher numeric values in the
390 @sc{ASCII} character set than their upper case counterparts; numbers and
391 many punctuation characters have a lower numeric value than upper case
392 letters.  A unibyte non-@sc{ASCII} character is always less than any
393 multibyte non-@sc{ASCII} character (@pxref{Text Representations}).
395 @example
396 @group
397 (string< "abc" "abd")
398      @result{} t
399 (string< "abd" "abc")
400      @result{} nil
401 (string< "123" "abc")
402      @result{} t
403 @end group
404 @end example
406 When the strings have different lengths, and they match up to the
407 length of @var{string1}, then the result is @code{t}.  If they match up
408 to the length of @var{string2}, the result is @code{nil}.  A string of
409 no characters is less than any other string.
411 @example
412 @group
413 (string< "" "abc")
414      @result{} t
415 (string< "ab" "abc")
416      @result{} t
417 (string< "abc" "")
418      @result{} nil
419 (string< "abc" "ab")
420      @result{} nil
421 (string< "" "")
422      @result{} nil                   
423 @end group
424 @end example
425 @end defun
427 @defun string-lessp string1 string2
428 @code{string-lessp} is another name for @code{string<}.
429 @end defun
431   See also @code{compare-buffer-substrings} in @ref{Comparing Text}, for
432 a way to compare text in buffers.  The function @code{string-match},
433 which matches a regular expression against a string, can be used
434 for a kind of string comparison; see @ref{Regexp Search}.
436 @node String Conversion
437 @comment  node-name,  next,  previous,  up
438 @section Conversion of Characters and Strings
439 @cindex conversion of strings
441   This section describes functions for conversions between characters,
442 strings and integers.  @code{format} and @code{prin1-to-string}
443 (@pxref{Output Functions}) can also convert Lisp objects into strings.
444 @code{read-from-string} (@pxref{Input Functions}) can ``convert'' a
445 string representation of a Lisp object into an object.  The functions
446 @code{string-make-multibyte} and @code{string-make-unibyte} convert the
447 text representation of a string (@pxref{Converting Representations}).
449   @xref{Documentation}, for functions that produce textual descriptions
450 of text characters and general input events
451 (@code{single-key-description} and @code{text-char-description}).  These
452 functions are used primarily for making help messages.
454 @defun char-to-string character
455 @cindex character to string
456   This function returns a new string with a length of one character.
457 The value of @var{character}, modulo 256, is used to initialize the
458 element of the string.
460 This function is similar to @code{make-string} with an integer argument
461 of 1.  (@xref{Creating Strings}.)  This conversion can also be done with
462 @code{format} using the @samp{%c} format specification.
463 (@xref{Formatting Strings}.)
465 @example
466 (char-to-string ?x)
467      @result{} "x"
468 (char-to-string (+ 256 ?x))
469      @result{} "x"
470 (make-string 1 ?x)
471      @result{} "x"
472 @end example
473 @end defun
475 @defun string-to-char string
476 @cindex string to character
477   This function returns the first character in @var{string}.  If the
478 string is empty, the function returns 0.  The value is also 0 when the
479 first character of @var{string} is the null character, @sc{ASCII} code
482 @example
483 (string-to-char "ABC")
484      @result{} 65
485 (string-to-char "xyz")
486      @result{} 120
487 (string-to-char "")
488      @result{} 0
489 (string-to-char "\000")
490      @result{} 0
491 @end example
493 This function may be eliminated in the future if it does not seem useful
494 enough to retain.
495 @end defun
497 @defun number-to-string number
498 @cindex integer to string
499 @cindex integer to decimal
500 This function returns a string consisting of the printed
501 representation of @var{number}, which may be an integer or a floating
502 point number.  The value starts with a sign if the argument is
503 negative.
505 @example
506 (number-to-string 256)
507      @result{} "256"
508 (number-to-string -23)
509      @result{} "-23"
510 (number-to-string -23.5)
511      @result{} "-23.5"
512 @end example
514 @cindex int-to-string
515 @code{int-to-string} is a semi-obsolete alias for this function.
517 See also the function @code{format} in @ref{Formatting Strings}.
518 @end defun
520 @defun string-to-number string base
521 @cindex string to number
522 This function returns the numeric value of the characters in
523 @var{string}.  If @var{base} is non-@code{nil}, integers are converted
524 in that base.  If @var{base} is @code{nil}, then base ten is used.
525 Floating point conversion always uses base ten; we have not implemented
526 other radices for floating point numbers, because that would be much
527 more work and does not seem useful.
529 The parsing skips spaces and tabs at the beginning of @var{string}, then
530 reads as much of @var{string} as it can interpret as a number.  (On some
531 systems it ignores other whitespace at the beginning, not just spaces
532 and tabs.)  If the first character after the ignored whitespace is not a
533 digit or a minus sign, this function returns 0.
535 @example
536 (string-to-number "256")
537      @result{} 256
538 (string-to-number "25 is a perfect square.")
539      @result{} 25
540 (string-to-number "X256")
541      @result{} 0
542 (string-to-number "-4.5")
543      @result{} -4.5
544 @end example
546 @findex string-to-int
547 @code{string-to-int} is an obsolete alias for this function.
548 @end defun
550   Here are some other functions that can convert to or from a string:
552 @table @code
553 @item concat
554 @code{concat} can convert a vector or a list into a string.
555 @xref{Creating Strings}.
557 @item vconcat
558 @code{vconcat} can convert a string into a vector.  @xref{Vector
559 Functions}.
561 @item append
562 @code{append} can convert a string into a list.  @xref{Building Lists}.
563 @end table
565 @node Formatting Strings
566 @comment  node-name,  next,  previous,  up
567 @section Formatting Strings
568 @cindex formatting strings
569 @cindex strings, formatting them
571   @dfn{Formatting} means constructing a string by substitution of
572 computed values at various places in a constant string.  This string
573 controls how the other values are printed as well as where they appear;
574 it is called a @dfn{format string}.
576   Formatting is often useful for computing messages to be displayed.  In
577 fact, the functions @code{message} and @code{error} provide the same
578 formatting feature described here; they differ from @code{format} only
579 in how they use the result of formatting.
581 @defun format string &rest objects
582   This function returns a new string that is made by copying
583 @var{string} and then replacing any format specification 
584 in the copy with encodings of the corresponding @var{objects}.  The
585 arguments @var{objects} are the computed values to be formatted.
586 @end defun
588 @cindex @samp{%} in format
589 @cindex format specification
590   A format specification is a sequence of characters beginning with a
591 @samp{%}.  Thus, if there is a @samp{%d} in @var{string}, the
592 @code{format} function replaces it with the printed representation of
593 one of the values to be formatted (one of the arguments @var{objects}).
594 For example:
596 @example
597 @group
598 (format "The value of fill-column is %d." fill-column)
599      @result{} "The value of fill-column is 72."
600 @end group
601 @end example
603   If @var{string} contains more than one format specification, the
604 format specifications correspond with successive values from
605 @var{objects}.  Thus, the first format specification in @var{string}
606 uses the first such value, the second format specification uses the
607 second such value, and so on.  Any extra format specifications (those
608 for which there are no corresponding values) cause unpredictable
609 behavior.  Any extra values to be formatted are ignored.
611   Certain format specifications require values of particular types.
612 However, no error is signaled if the value actually supplied fails to
613 have the expected type.  Instead, the output is likely to be
614 meaningless.
616   Here is a table of valid format specifications:
618 @table @samp
619 @item %s
620 Replace the specification with the printed representation of the object,
621 made without quoting (that is, using @code{princ}, not
622 @code{print}---@pxref{Output Functions}).  Thus, strings are represented
623 by their contents alone, with no @samp{"} characters, and symbols appear
624 without @samp{\} characters.
626 If there is no corresponding object, the empty string is used.
628 @item %S
629 Replace the specification with the printed representation of the object,
630 made with quoting (that is, using @code{prin1}---@pxref{Output
631 Functions}).  Thus, strings are enclosed in @samp{"} characters, and
632 @samp{\} characters appear where necessary before special characters.
634 If there is no corresponding object, the empty string is used.
636 @item %o
637 @cindex integer to octal
638 Replace the specification with the base-eight representation of an
639 integer.
641 @item %d
642 Replace the specification with the base-ten representation of an
643 integer.
645 @item %x
646 @cindex integer to hexadecimal
647 Replace the specification with the base-sixteen representation of an
648 integer.
650 @item %c
651 Replace the specification with the character which is the value given.
653 @item %e
654 Replace the specification with the exponential notation for a floating
655 point number.
657 @item %f
658 Replace the specification with the decimal-point notation for a floating
659 point number.
661 @item %g
662 Replace the specification with notation for a floating point number,
663 using either exponential notation or decimal-point notation whichever
664 is shorter.
666 @item %%
667 A single @samp{%} is placed in the string.  This format specification is
668 unusual in that it does not use a value.  For example, @code{(format "%%
669 %d" 30)} returns @code{"% 30"}.
670 @end table
672   Any other format character results in an @samp{Invalid format
673 operation} error.
675   Here are several examples:
677 @example
678 @group
679 (format "The name of this buffer is %s." (buffer-name))
680      @result{} "The name of this buffer is strings.texi."
682 (format "The buffer object prints as %s." (current-buffer))
683      @result{} "The buffer object prints as strings.texi."
685 (format "The octal value of %d is %o, 
686          and the hex value is %x." 18 18 18)
687      @result{} "The octal value of 18 is 22, 
688          and the hex value is 12."
689 @end group
690 @end example
692 @cindex numeric prefix
693 @cindex field width
694 @cindex padding
695   All the specification characters allow an optional numeric prefix
696 between the @samp{%} and the character.  The optional numeric prefix
697 defines the minimum width for the object.  If the printed representation
698 of the object contains fewer characters than this, then it is padded.
699 The padding is on the left if the prefix is positive (or starts with
700 zero) and on the right if the prefix is negative.  The padding character
701 is normally a space, but if the numeric prefix starts with a zero, zeros
702 are used for padding.  Here are some examples of padding:
704 @example
705 (format "%06d is padded on the left with zeros" 123)
706      @result{} "000123 is padded on the left with zeros"
708 (format "%-6d is padded on the right" 123)
709      @result{} "123    is padded on the right"
710 @end example
712   @code{format} never truncates an object's printed representation, no
713 matter what width you specify.  Thus, you can use a numeric prefix to
714 specify a minimum spacing between columns with no risk of losing
715 information.
717   In the following three examples, @samp{%7s} specifies a minimum width
718 of 7.  In the first case, the string inserted in place of @samp{%7s} has
719 only 3 letters, so 4 blank spaces are inserted for padding.  In the
720 second case, the string @code{"specification"} is 13 letters wide but is
721 not truncated.  In the third case, the padding is on the right.
723 @smallexample 
724 @group
725 (format "The word `%7s' actually has %d letters in it."
726         "foo" (length "foo"))
727      @result{} "The word `    foo' actually has 3 letters in it."  
728 @end group
730 @group
731 (format "The word `%7s' actually has %d letters in it."
732         "specification" (length "specification")) 
733      @result{} "The word `specification' actually has 13 letters in it."  
734 @end group
736 @group
737 (format "The word `%-7s' actually has %d letters in it."
738         "foo" (length "foo"))
739      @result{} "The word `foo    ' actually has 3 letters in it."  
740 @end group
741 @end smallexample
743 @node Character Case
744 @comment node-name, next, previous, up 
745 @section Character Case
746 @cindex upper case 
747 @cindex lower case 
748 @cindex character case 
750   The character case functions change the case of single characters or
751 of the contents of strings.  The functions convert only alphabetic
752 characters (the letters @samp{A} through @samp{Z} and @samp{a} through
753 @samp{z}); other characters are not altered.  The functions do not
754 modify the strings that are passed to them as arguments.
756   The examples below use the characters @samp{X} and @samp{x} which have
757 @sc{ASCII} codes 88 and 120 respectively.
759 @defun downcase string-or-char
760 This function converts a character or a string to lower case.
762 When the argument to @code{downcase} is a string, the function creates
763 and returns a new string in which each letter in the argument that is
764 upper case is converted to lower case.  When the argument to
765 @code{downcase} is a character, @code{downcase} returns the
766 corresponding lower case character.  This value is an integer.  If the
767 original character is lower case, or is not a letter, then the value
768 equals the original character.
770 @example
771 (downcase "The cat in the hat")
772      @result{} "the cat in the hat"
774 (downcase ?X)
775      @result{} 120
776 @end example
777 @end defun
779 @defun upcase string-or-char
780 This function converts a character or a string to upper case.
782 When the argument to @code{upcase} is a string, the function creates
783 and returns a new string in which each letter in the argument that is
784 lower case is converted to upper case.
786 When the argument to @code{upcase} is a character, @code{upcase}
787 returns the corresponding upper case character.  This value is an integer.
788 If the original character is upper case, or is not a letter, then the
789 value equals the original character.
791 @example
792 (upcase "The cat in the hat")
793      @result{} "THE CAT IN THE HAT"
795 (upcase ?x)
796      @result{} 88
797 @end example
798 @end defun
800 @defun capitalize string-or-char
801 @cindex capitalization
802 This function capitalizes strings or characters.  If
803 @var{string-or-char} is a string, the function creates and returns a new
804 string, whose contents are a copy of @var{string-or-char} in which each
805 word has been capitalized.  This means that the first character of each
806 word is converted to upper case, and the rest are converted to lower
807 case.
809 The definition of a word is any sequence of consecutive characters that
810 are assigned to the word constituent syntax class in the current syntax
811 table (@xref{Syntax Class Table}).
813 When the argument to @code{capitalize} is a character, @code{capitalize}
814 has the same result as @code{upcase}.
816 @example
817 (capitalize "The cat in the hat")
818      @result{} "The Cat In The Hat"
820 (capitalize "THE 77TH-HATTED CAT")
821      @result{} "The 77th-Hatted Cat"
823 @group
824 (capitalize ?x)
825      @result{} 88
826 @end group
827 @end example
828 @end defun
830 @node Case Table
831 @section The Case Table
833   You can customize case conversion by installing a special @dfn{case
834 table}.  A case table specifies the mapping between upper case and lower
835 case letters.  It affects both the string and character case conversion
836 functions (see the previous section) and those that apply to text in the
837 buffer (@pxref{Case Changes}).
839   A case table is a char-table whose subtype is @code{case-table}.  This
840 char-table maps each character into the corresponding lower case
841 character  It has three extra slots, which are related tables:
843 @table @var
844 @item upcase
845 The upcase table maps each character into the corresponding upper
846 case character.
847 @item canonicalize
848 The canonicalize table maps all of a set of case-related characters
849 into some one of them.
850 @item equivalences
851 The equivalences table maps each of a set of case-related characters
852 into the next one in that set.
853 @end table
855   In simple cases, all you need to specify is the mapping to lower-case;
856 the three related tables will be calculated automatically from that one.
858   For some languages, upper and lower case letters are not in one-to-one
859 correspondence.  There may be two different lower case letters with the
860 same upper case equivalent.  In these cases, you need to specify the
861 maps for both lower case and upper case.
863   The extra table @var{canonicalize} maps each character to a canonical
864 equivalent; any two characters that are related by case-conversion have
865 the same canonical equivalent character.  For example, since @samp{a}
866 and @samp{A} are related by case-conversion, they should have the same
867 canonical equivalent character (which should be either @samp{a} for both
868 of them, or @samp{A} for both of them).
870   The extra table @var{equivalences} is a map that cyclicly permutes
871 each equivalence class (of characters with the same canonical
872 equivalent).  (For ordinary @sc{ASCII}, this would map @samp{a} into
873 @samp{A} and @samp{A} into @samp{a}, and likewise for each set of
874 equivalent characters.)
876   When you construct a case table, you can provide @code{nil} for
877 @var{canonicalize}; then Emacs fills in this string from the lower case
878 and upper case mappings.  You can also provide @code{nil} for
879 @var{equivalences}; then Emacs fills in this string from
880 @var{canonicalize}.  In a case table that is actually in use, those
881 components are non-@code{nil}.  Do not try to specify @var{equivalences}
882 without also specifying @var{canonicalize}.
884   Each buffer has a case table.  Emacs also has a @dfn{standard case
885 table} which is copied into each buffer when you create the buffer.
886 Changing the standard case table doesn't affect any existing buffers.
888   Here are the functions for working with case tables:
890 @defun case-table-p object
891 This predicate returns non-@code{nil} if @var{object} is a valid case
892 table.
893 @end defun
895 @defun set-standard-case-table table
896 This function makes @var{table} the standard case table, so that it will
897 apply to any buffers created subsequently.
898 @end defun
900 @defun standard-case-table
901 This returns the standard case table.
902 @end defun
904 @defun current-case-table
905 This function returns the current buffer's case table.
906 @end defun
908 @defun set-case-table table
909 This sets the current buffer's case table to @var{table}.
910 @end defun
912   The following three functions are convenient subroutines for packages
913 that define non-@sc{ASCII} character sets.  They modify the specified
914 case table @var{case-table}; they also modify the standard syntax table.
915 @xref{Syntax Tables}.
917 @defun set-case-syntax-pair uc lc case-table
918 This function specifies a pair of corresponding letters, one upper case
919 and one lower case.
920 @end defun
922 @defun set-case-syntax-delims l r case-table
923 This function makes characters @var{l} and @var{r} a matching pair of
924 case-invariant delimiters.
925 @end defun
927 @defun set-case-syntax char syntax case-table
928 This function makes @var{char} case-invariant, with syntax
929 @var{syntax}.
930 @end defun
932 @deffn Command describe-buffer-case-table
933 This command displays a description of the contents of the current
934 buffer's case table.
935 @end deffn