(File Name Components): Fix file-name-extension documentation.
[bpt/emacs.git] / doc / lispref / strings.texi
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, 1999, 2001,
4 @c 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../../info/strings
7 @node Strings and Characters, Lists, Numbers, Top
8 @comment node-name, next, previous, up
9 @chapter Strings and Characters
10 @cindex strings
11 @cindex character arrays
12 @cindex characters
13 @cindex bytes
14
15 A string in Emacs Lisp is an array that contains an ordered sequence
16 of characters. Strings are used as names of symbols, buffers, and
17 files; to send messages to users; to hold text being copied between
18 buffers; and for many other purposes. Because strings are so important,
19 Emacs Lisp has many functions expressly for manipulating them. Emacs
20 Lisp programs use strings more often than individual characters.
21
22 @xref{Strings of Events}, for special considerations for strings of
23 keyboard character events.
24
25 @menu
26 * Basics: String Basics. Basic properties of strings and characters.
27 * Predicates for Strings:: Testing whether an object is a string or char.
28 * Creating Strings:: Functions to allocate new strings.
29 * Modifying Strings:: Altering the contents of an existing string.
30 * Text Comparison:: Comparing characters or strings.
31 * String Conversion:: Converting to and from characters and strings.
32 * Formatting Strings:: @code{format}: Emacs's analogue of @code{printf}.
33 * Case Conversion:: Case conversion functions.
34 * Case Tables:: Customizing case conversion.
35 @end menu
36
37 @node String Basics
38 @section String and Character Basics
39
40 Characters are represented in Emacs Lisp as integers;
41 whether an integer is a character or not is determined only by how it is
42 used. Thus, strings really contain integers.
43
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 @acronym{ASCII} code 0.)
48
49 Since strings are arrays, and therefore sequences as well, you can
50 operate on them with the general array and sequence functions.
51 (@xref{Sequences Arrays Vectors}.) For example, you can access or
52 change individual characters in a string using the functions @code{aref}
53 and @code{aset} (@pxref{Array Functions}).
54
55 There are two text representations for non-@acronym{ASCII} characters in
56 Emacs strings (and in buffers): unibyte and multibyte (@pxref{Text
57 Representations}). An @acronym{ASCII} character always occupies one byte in a
58 string; in fact, when a string is all @acronym{ASCII}, there is no real
59 difference between the unibyte and multibyte representations.
60 For most Lisp programming, you don't need to be concerned with these two
61 representations.
62
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 large integers) rather than character
66 codes in the range 128 to 255.
67
68 Strings cannot hold characters that have the hyper, super or alt
69 modifiers; they can hold @acronym{ASCII} control characters, but no other
70 control characters. They do not distinguish case in @acronym{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 the representation of meta
74 and other modifiers for keyboard input characters.
75
76 Strings are useful for holding regular expressions. You can also
77 match regular expressions against strings with @code{string-match}
78 (@pxref{Regexp Search}). The functions @code{match-string}
79 (@pxref{Simple Match Data}) and @code{replace-match} (@pxref{Replacing
80 Match}) are useful for decomposing and modifying strings after
81 matching regular expressions against them.
82
83 Like a buffer, a string can contain text properties for the characters
84 in it, as well as the characters themselves. @xref{Text Properties}.
85 All the Lisp primitives that copy text from strings to buffers or other
86 strings also copy the properties of the characters being copied.
87
88 @xref{Text}, for information about functions that display strings or
89 copy them into buffers. @xref{Character Type}, and @ref{String Type},
90 for information about the syntax of characters and strings.
91 @xref{Non-ASCII Characters}, for functions to convert between text
92 representations and to encode and decode character codes.
93
94 @node Predicates for Strings
95 @section The Predicates for Strings
96
97 For more information about general sequence and array predicates,
98 see @ref{Sequences Arrays Vectors}, and @ref{Arrays}.
99
100 @defun stringp object
101 This function returns @code{t} if @var{object} is a string, @code{nil}
102 otherwise.
103 @end defun
104
105 @defun string-or-null-p object
106 This function returns @code{t} if @var{object} is a string or nil,
107 @code{nil} otherwise.
108 @end defun
109
110 @defun char-or-string-p object
111 This function returns @code{t} if @var{object} is a string or a
112 character (i.e., an integer), @code{nil} otherwise.
113 @end defun
114
115 @node Creating Strings
116 @section Creating Strings
117
118 The following functions create strings, either from scratch, or by
119 putting strings together, or by taking them apart.
120
121 @defun make-string count character
122 This function returns a string made up of @var{count} repetitions of
123 @var{character}. If @var{count} is negative, an error is signaled.
124
125 @example
126 (make-string 5 ?x)
127 @result{} "xxxxx"
128 (make-string 0 ?x)
129 @result{} ""
130 @end example
131
132 Other functions to compare with this one include @code{char-to-string}
133 (@pxref{String Conversion}), @code{make-vector} (@pxref{Vectors}), and
134 @code{make-list} (@pxref{Building Lists}).
135 @end defun
136
137 @defun string &rest characters
138 This returns a string containing the characters @var{characters}.
139
140 @example
141 (string ?a ?b ?c)
142 @result{} "abc"
143 @end example
144 @end defun
145
146 @defun substring string start &optional end
147 This function returns a new string which consists of those characters
148 from @var{string} in the range from (and including) the character at the
149 index @var{start} up to (but excluding) the character at the index
150 @var{end}. The first character is at index zero.
151
152 @example
153 @group
154 (substring "abcdefg" 0 3)
155 @result{} "abc"
156 @end group
157 @end example
158
159 @noindent
160 Here the index for @samp{a} is 0, the index for @samp{b} is 1, and the
161 index for @samp{c} is 2. Thus, three letters, @samp{abc}, are copied
162 from the string @code{"abcdefg"}. The index 3 marks the character
163 position up to which the substring is copied. The character whose index
164 is 3 is actually the fourth character in the string.
165
166 A negative number counts from the end of the string, so that @minus{}1
167 signifies the index of the last character of the string. For example:
168
169 @example
170 @group
171 (substring "abcdefg" -3 -1)
172 @result{} "ef"
173 @end group
174 @end example
175
176 @noindent
177 In this example, the index for @samp{e} is @minus{}3, the index for
178 @samp{f} is @minus{}2, and the index for @samp{g} is @minus{}1.
179 Therefore, @samp{e} and @samp{f} are included, and @samp{g} is excluded.
180
181 When @code{nil} is used for @var{end}, it stands for the length of the
182 string. Thus,
183
184 @example
185 @group
186 (substring "abcdefg" -3 nil)
187 @result{} "efg"
188 @end group
189 @end example
190
191 Omitting the argument @var{end} is equivalent to specifying @code{nil}.
192 It follows that @code{(substring @var{string} 0)} returns a copy of all
193 of @var{string}.
194
195 @example
196 @group
197 (substring "abcdefg" 0)
198 @result{} "abcdefg"
199 @end group
200 @end example
201
202 @noindent
203 But we recommend @code{copy-sequence} for this purpose (@pxref{Sequence
204 Functions}).
205
206 If the characters copied from @var{string} have text properties, the
207 properties are copied into the new string also. @xref{Text Properties}.
208
209 @code{substring} also accepts a vector for the first argument.
210 For example:
211
212 @example
213 (substring [a b (c) "d"] 1 3)
214 @result{} [b (c)]
215 @end example
216
217 A @code{wrong-type-argument} error is signaled if @var{start} is not
218 an integer or if @var{end} is neither an integer nor @code{nil}. An
219 @code{args-out-of-range} error is signaled if @var{start} indicates a
220 character following @var{end}, or if either integer is out of range
221 for @var{string}.
222
223 Contrast this function with @code{buffer-substring} (@pxref{Buffer
224 Contents}), which returns a string containing a portion of the text in
225 the current buffer. The beginning of a string is at index 0, but the
226 beginning of a buffer is at index 1.
227 @end defun
228
229 @defun substring-no-properties string &optional start end
230 This works like @code{substring} but discards all text properties from
231 the value. Also, @var{start} may be omitted or @code{nil}, which is
232 equivalent to 0. Thus, @w{@code{(substring-no-properties
233 @var{string})}} returns a copy of @var{string}, with all text
234 properties removed.
235 @end defun
236
237 @defun concat &rest sequences
238 @cindex copying strings
239 @cindex concatenating strings
240 This function returns a new string consisting of the characters in the
241 arguments passed to it (along with their text properties, if any). The
242 arguments may be strings, lists of numbers, or vectors of numbers; they
243 are not themselves changed. If @code{concat} receives no arguments, it
244 returns an empty string.
245
246 @example
247 (concat "abc" "-def")
248 @result{} "abc-def"
249 (concat "abc" (list 120 121) [122])
250 @result{} "abcxyz"
251 ;; @r{@code{nil} is an empty sequence.}
252 (concat "abc" nil "-def")
253 @result{} "abc-def"
254 (concat "The " "quick brown " "fox.")
255 @result{} "The quick brown fox."
256 (concat)
257 @result{} ""
258 @end example
259
260 @noindent
261 The @code{concat} function always constructs a new string that is
262 not @code{eq} to any existing string, except when the result is empty
263 (since empty strings are canonicalized to save space).
264
265 In Emacs versions before 21, when an argument was an integer (not a
266 sequence of integers), it was converted to a string of digits making up
267 the decimal printed representation of the integer. This obsolete usage
268 no longer works. The proper way to convert an integer to its decimal
269 printed form is with @code{format} (@pxref{Formatting Strings}) or
270 @code{number-to-string} (@pxref{String Conversion}).
271
272 For information about other concatenation functions, see the
273 description of @code{mapconcat} in @ref{Mapping Functions},
274 @code{vconcat} in @ref{Vector Functions}, and @code{append} in @ref{Building
275 Lists}. For concatenating individual command-line arguments into a
276 string to be used as a shell command, see @ref{Shell Arguments,
277 combine-and-quote-strings}.
278 @end defun
279
280 @defun split-string string &optional separators omit-nulls
281 This function splits @var{string} into substrings at matches for the
282 regular expression @var{separators}. Each match for @var{separators}
283 defines a splitting point; the substrings between the splitting points
284 are made into a list, which is the value returned by
285 @code{split-string}.
286
287 If @var{omit-nulls} is @code{nil}, the result contains null strings
288 whenever there are two consecutive matches for @var{separators}, or a
289 match is adjacent to the beginning or end of @var{string}. If
290 @var{omit-nulls} is @code{t}, these null strings are omitted from the
291 result.
292
293 If @var{separators} is @code{nil} (or omitted),
294 the default is the value of @code{split-string-default-separators}.
295
296 As a special case, when @var{separators} is @code{nil} (or omitted),
297 null strings are always omitted from the result. Thus:
298
299 @example
300 (split-string " two words ")
301 @result{} ("two" "words")
302 @end example
303
304 The result is not @code{("" "two" "words" "")}, which would rarely be
305 useful. If you need such a result, use an explicit value for
306 @var{separators}:
307
308 @example
309 (split-string " two words "
310 split-string-default-separators)
311 @result{} ("" "two" "words" "")
312 @end example
313
314 More examples:
315
316 @example
317 (split-string "Soup is good food" "o")
318 @result{} ("S" "up is g" "" "d f" "" "d")
319 (split-string "Soup is good food" "o" t)
320 @result{} ("S" "up is g" "d f" "d")
321 (split-string "Soup is good food" "o+")
322 @result{} ("S" "up is g" "d f" "d")
323 @end example
324
325 Empty matches do count, except that @code{split-string} will not look
326 for a final empty match when it already reached the end of the string
327 using a non-empty match or when @var{string} is empty:
328
329 @example
330 (split-string "aooob" "o*")
331 @result{} ("" "a" "" "b" "")
332 (split-string "ooaboo" "o*")
333 @result{} ("" "" "a" "b" "")
334 (split-string "" "")
335 @result{} ("")
336 @end example
337
338 However, when @var{separators} can match the empty string,
339 @var{omit-nulls} is usually @code{t}, so that the subtleties in the
340 three previous examples are rarely relevant:
341
342 @example
343 (split-string "Soup is good food" "o*" t)
344 @result{} ("S" "u" "p" " " "i" "s" " " "g" "d" " " "f" "d")
345 (split-string "Nice doggy!" "" t)
346 @result{} ("N" "i" "c" "e" " " "d" "o" "g" "g" "y" "!")
347 (split-string "" "" t)
348 @result{} nil
349 @end example
350
351 Somewhat odd, but predictable, behavior can occur for certain
352 ``non-greedy'' values of @var{separators} that can prefer empty
353 matches over non-empty matches. Again, such values rarely occur in
354 practice:
355
356 @example
357 (split-string "ooo" "o*" t)
358 @result{} nil
359 (split-string "ooo" "\\|o+" t)
360 @result{} ("o" "o" "o")
361 @end example
362
363 If you need to split a string that is a shell command, where
364 individual arguments could be quoted, see @ref{Shell Arguments,
365 split-string-and-unquote}.
366 @end defun
367
368 @defvar split-string-default-separators
369 The default value of @var{separators} for @code{split-string}. Its
370 usual value is @w{@code{"[ \f\t\n\r\v]+"}}.
371 @end defvar
372
373 @node Modifying Strings
374 @section Modifying Strings
375
376 The most basic way to alter the contents of an existing string is with
377 @code{aset} (@pxref{Array Functions}). @code{(aset @var{string}
378 @var{idx} @var{char})} stores @var{char} into @var{string} at index
379 @var{idx}. Each character occupies one or more bytes, and if @var{char}
380 needs a different number of bytes from the character already present at
381 that index, @code{aset} signals an error.
382
383 A more powerful function is @code{store-substring}:
384
385 @defun store-substring string idx obj
386 This function alters part of the contents of the string @var{string}, by
387 storing @var{obj} starting at index @var{idx}. The argument @var{obj}
388 may be either a character or a (smaller) string.
389
390 Since it is impossible to change the length of an existing string, it is
391 an error if @var{obj} doesn't fit within @var{string}'s actual length,
392 or if any new character requires a different number of bytes from the
393 character currently present at that point in @var{string}.
394 @end defun
395
396 To clear out a string that contained a password, use
397 @code{clear-string}:
398
399 @defun clear-string string
400 This makes @var{string} a unibyte string and clears its contents to
401 zeros. It may also change @var{string}'s length.
402 @end defun
403
404 @need 2000
405 @node Text Comparison
406 @section Comparison of Characters and Strings
407 @cindex string equality
408
409 @defun char-equal character1 character2
410 This function returns @code{t} if the arguments represent the same
411 character, @code{nil} otherwise. This function ignores differences
412 in case if @code{case-fold-search} is non-@code{nil}.
413
414 @example
415 (char-equal ?x ?x)
416 @result{} t
417 (let ((case-fold-search nil))
418 (char-equal ?x ?X))
419 @result{} nil
420 @end example
421 @end defun
422
423 @defun string= string1 string2
424 This function returns @code{t} if the characters of the two strings
425 match exactly. Symbols are also allowed as arguments, in which case
426 their print names are used.
427 Case is always significant, regardless of @code{case-fold-search}.
428
429 @example
430 (string= "abc" "abc")
431 @result{} t
432 (string= "abc" "ABC")
433 @result{} nil
434 (string= "ab" "ABC")
435 @result{} nil
436 @end example
437
438 The function @code{string=} ignores the text properties of the two
439 strings. When @code{equal} (@pxref{Equality Predicates}) compares two
440 strings, it uses @code{string=}.
441
442 For technical reasons, a unibyte and a multibyte string are
443 @code{equal} if and only if they contain the same sequence of
444 character codes and all these codes are either in the range 0 through
445 127 (@acronym{ASCII}) or 160 through 255 (@code{eight-bit-graphic}).
446 However, when a unibyte string gets converted to a multibyte string,
447 all characters with codes in the range 160 through 255 get converted
448 to characters with higher codes, whereas @acronym{ASCII} characters
449 remain unchanged. Thus, a unibyte string and its conversion to
450 multibyte are only @code{equal} if the string is all @acronym{ASCII}.
451 Character codes 160 through 255 are not entirely proper in multibyte
452 text, even though they can occur. As a consequence, the situation
453 where a unibyte and a multibyte string are @code{equal} without both
454 being all @acronym{ASCII} is a technical oddity that very few Emacs
455 Lisp programmers ever get confronted with. @xref{Text
456 Representations}.
457 @end defun
458
459 @defun string-equal string1 string2
460 @code{string-equal} is another name for @code{string=}.
461 @end defun
462
463 @cindex lexical comparison
464 @defun string< string1 string2
465 @c (findex string< causes problems for permuted index!!)
466 This function compares two strings a character at a time. It
467 scans both the strings at the same time to find the first pair of corresponding
468 characters that do not match. If the lesser character of these two is
469 the character from @var{string1}, then @var{string1} is less, and this
470 function returns @code{t}. If the lesser character is the one from
471 @var{string2}, then @var{string1} is greater, and this function returns
472 @code{nil}. If the two strings match entirely, the value is @code{nil}.
473
474 Pairs of characters are compared according to their character codes.
475 Keep in mind that lower case letters have higher numeric values in the
476 @acronym{ASCII} character set than their upper case counterparts; digits and
477 many punctuation characters have a lower numeric value than upper case
478 letters. An @acronym{ASCII} character is less than any non-@acronym{ASCII}
479 character; a unibyte non-@acronym{ASCII} character is always less than any
480 multibyte non-@acronym{ASCII} character (@pxref{Text Representations}).
481
482 @example
483 @group
484 (string< "abc" "abd")
485 @result{} t
486 (string< "abd" "abc")
487 @result{} nil
488 (string< "123" "abc")
489 @result{} t
490 @end group
491 @end example
492
493 When the strings have different lengths, and they match up to the
494 length of @var{string1}, then the result is @code{t}. If they match up
495 to the length of @var{string2}, the result is @code{nil}. A string of
496 no characters is less than any other string.
497
498 @example
499 @group
500 (string< "" "abc")
501 @result{} t
502 (string< "ab" "abc")
503 @result{} t
504 (string< "abc" "")
505 @result{} nil
506 (string< "abc" "ab")
507 @result{} nil
508 (string< "" "")
509 @result{} nil
510 @end group
511 @end example
512
513 Symbols are also allowed as arguments, in which case their print names
514 are used.
515 @end defun
516
517 @defun string-lessp string1 string2
518 @code{string-lessp} is another name for @code{string<}.
519 @end defun
520
521 @defun compare-strings string1 start1 end1 string2 start2 end2 &optional ignore-case
522 This function compares the specified part of @var{string1} with the
523 specified part of @var{string2}. The specified part of @var{string1}
524 runs from index @var{start1} up to index @var{end1} (@code{nil} means
525 the end of the string). The specified part of @var{string2} runs from
526 index @var{start2} up to index @var{end2} (@code{nil} means the end of
527 the string).
528
529 The strings are both converted to multibyte for the comparison
530 (@pxref{Text Representations}) so that a unibyte string and its
531 conversion to multibyte are always regarded as equal. If
532 @var{ignore-case} is non-@code{nil}, then case is ignored, so that
533 upper case letters can be equal to lower case letters.
534
535 If the specified portions of the two strings match, the value is
536 @code{t}. Otherwise, the value is an integer which indicates how many
537 leading characters agree, and which string is less. Its absolute value
538 is one plus the number of characters that agree at the beginning of the
539 two strings. The sign is negative if @var{string1} (or its specified
540 portion) is less.
541 @end defun
542
543 @defun assoc-string key alist &optional case-fold
544 This function works like @code{assoc}, except that @var{key} must be a
545 string or symbol, and comparison is done using @code{compare-strings}.
546 Symbols are converted to strings before testing.
547 If @var{case-fold} is non-@code{nil}, it ignores case differences.
548 Unlike @code{assoc}, this function can also match elements of the alist
549 that are strings or symbols rather than conses. In particular, @var{alist} can
550 be a list of strings or symbols rather than an actual alist.
551 @xref{Association Lists}.
552 @end defun
553
554 See also the @code{compare-buffer-substrings} function in
555 @ref{Comparing Text}, for a way to compare text in buffers. The
556 function @code{string-match}, which matches a regular expression
557 against a string, can be used for a kind of string comparison; see
558 @ref{Regexp Search}.
559
560 @node String Conversion
561 @comment node-name, next, previous, up
562 @section Conversion of Characters and Strings
563 @cindex conversion of strings
564
565 This section describes functions for conversions between characters,
566 strings and integers. @code{format} (@pxref{Formatting Strings})
567 and @code{prin1-to-string}
568 (@pxref{Output Functions}) can also convert Lisp objects into strings.
569 @code{read-from-string} (@pxref{Input Functions}) can ``convert'' a
570 string representation of a Lisp object into an object. The functions
571 @code{string-make-multibyte} and @code{string-make-unibyte} convert the
572 text representation of a string (@pxref{Converting Representations}).
573
574 @xref{Documentation}, for functions that produce textual descriptions
575 of text characters and general input events
576 (@code{single-key-description} and @code{text-char-description}). These
577 are used primarily for making help messages.
578
579 @defun char-to-string character
580 @cindex character to string
581 This function returns a new string containing one character,
582 @var{character}. This function is semi-obsolete because the function
583 @code{string} is more general. @xref{Creating Strings}.
584 @end defun
585
586 @defun string-to-char string
587 @cindex string to character
588 This function returns the first character in @var{string}. If the
589 string is empty, the function returns 0. The value is also 0 when the
590 first character of @var{string} is the null character, @acronym{ASCII} code
591 0.
592
593 @example
594 (string-to-char "ABC")
595 @result{} 65
596
597 (string-to-char "xyz")
598 @result{} 120
599 (string-to-char "")
600 @result{} 0
601 @group
602 (string-to-char "\000")
603 @result{} 0
604 @end group
605 @end example
606
607 This function may be eliminated in the future if it does not seem useful
608 enough to retain.
609 @end defun
610
611 @defun number-to-string number
612 @cindex integer to string
613 @cindex integer to decimal
614 This function returns a string consisting of the printed base-ten
615 representation of @var{number}, which may be an integer or a floating
616 point number. The returned value starts with a minus sign if the argument is
617 negative.
618
619 @example
620 (number-to-string 256)
621 @result{} "256"
622 @group
623 (number-to-string -23)
624 @result{} "-23"
625 @end group
626 (number-to-string -23.5)
627 @result{} "-23.5"
628 @end example
629
630 @cindex int-to-string
631 @code{int-to-string} is a semi-obsolete alias for this function.
632
633 See also the function @code{format} in @ref{Formatting Strings}.
634 @end defun
635
636 @defun string-to-number string &optional base
637 @cindex string to number
638 This function returns the numeric value of the characters in
639 @var{string}. If @var{base} is non-@code{nil}, it must be an integer
640 between 2 and 16 (inclusive), and integers are converted in that base.
641 If @var{base} is @code{nil}, then base ten is used. Floating point
642 conversion only works in base ten; we have not implemented other
643 radices for floating point numbers, because that would be much more
644 work and does not seem useful. If @var{string} looks like an integer
645 but its value is too large to fit into a Lisp integer,
646 @code{string-to-number} returns a floating point result.
647
648 The parsing skips spaces and tabs at the beginning of @var{string},
649 then reads as much of @var{string} as it can interpret as a number in
650 the given base. (On some systems it ignores other whitespace at the
651 beginning, not just spaces and tabs.) If the first character after
652 the ignored whitespace is neither a digit in the given base, nor a
653 plus or minus sign, nor the leading dot of a floating point number,
654 this function returns 0.
655
656 @example
657 (string-to-number "256")
658 @result{} 256
659 (string-to-number "25 is a perfect square.")
660 @result{} 25
661 (string-to-number "X256")
662 @result{} 0
663 (string-to-number "-4.5")
664 @result{} -4.5
665 (string-to-number "1e5")
666 @result{} 100000.0
667 @end example
668
669 @findex string-to-int
670 @code{string-to-int} is an obsolete alias for this function.
671 @end defun
672
673 Here are some other functions that can convert to or from a string:
674
675 @table @code
676 @item concat
677 @code{concat} can convert a vector or a list into a string.
678 @xref{Creating Strings}.
679
680 @item vconcat
681 @code{vconcat} can convert a string into a vector. @xref{Vector
682 Functions}.
683
684 @item append
685 @code{append} can convert a string into a list. @xref{Building Lists}.
686 @end table
687
688 @node Formatting Strings
689 @comment node-name, next, previous, up
690 @section Formatting Strings
691 @cindex formatting strings
692 @cindex strings, formatting them
693
694 @dfn{Formatting} means constructing a string by substitution of
695 computed values at various places in a constant string. This constant string
696 controls how the other values are printed, as well as where they appear;
697 it is called a @dfn{format string}.
698
699 Formatting is often useful for computing messages to be displayed. In
700 fact, the functions @code{message} and @code{error} provide the same
701 formatting feature described here; they differ from @code{format} only
702 in how they use the result of formatting.
703
704 @defun format string &rest objects
705 This function returns a new string that is made by copying
706 @var{string} and then replacing any format specification
707 in the copy with encodings of the corresponding @var{objects}. The
708 arguments @var{objects} are the computed values to be formatted.
709
710 The characters in @var{string}, other than the format specifications,
711 are copied directly into the output, including their text properties,
712 if any.
713 @end defun
714
715 @cindex @samp{%} in format
716 @cindex format specification
717 A format specification is a sequence of characters beginning with a
718 @samp{%}. Thus, if there is a @samp{%d} in @var{string}, the
719 @code{format} function replaces it with the printed representation of
720 one of the values to be formatted (one of the arguments @var{objects}).
721 For example:
722
723 @example
724 @group
725 (format "The value of fill-column is %d." fill-column)
726 @result{} "The value of fill-column is 72."
727 @end group
728 @end example
729
730 Since @code{format} interprets @samp{%} characters as format
731 specifications, you should @emph{never} pass an arbitrary string as
732 the first argument. This is particularly true when the string is
733 generated by some Lisp code. Unless the string is @emph{known} to
734 never include any @samp{%} characters, pass @code{"%s"}, described
735 below, as the first argument, and the string as the second, like this:
736
737 @example
738 (format "%s" @var{arbitrary-string})
739 @end example
740
741 If @var{string} contains more than one format specification, the
742 format specifications correspond to successive values from
743 @var{objects}. Thus, the first format specification in @var{string}
744 uses the first such value, the second format specification uses the
745 second such value, and so on. Any extra format specifications (those
746 for which there are no corresponding values) cause an error. Any
747 extra values to be formatted are ignored.
748
749 Certain format specifications require values of particular types. If
750 you supply a value that doesn't fit the requirements, an error is
751 signaled.
752
753 Here is a table of valid format specifications:
754
755 @table @samp
756 @item %s
757 Replace the specification with the printed representation of the object,
758 made without quoting (that is, using @code{princ}, not
759 @code{prin1}---@pxref{Output Functions}). Thus, strings are represented
760 by their contents alone, with no @samp{"} characters, and symbols appear
761 without @samp{\} characters.
762
763 If the object is a string, its text properties are
764 copied into the output. The text properties of the @samp{%s} itself
765 are also copied, but those of the object take priority.
766
767 @item %S
768 Replace the specification with the printed representation of the object,
769 made with quoting (that is, using @code{prin1}---@pxref{Output
770 Functions}). Thus, strings are enclosed in @samp{"} characters, and
771 @samp{\} characters appear where necessary before special characters.
772
773 @item %o
774 @cindex integer to octal
775 Replace the specification with the base-eight representation of an
776 integer.
777
778 @item %d
779 Replace the specification with the base-ten representation of an
780 integer.
781
782 @item %x
783 @itemx %X
784 @cindex integer to hexadecimal
785 Replace the specification with the base-sixteen representation of an
786 integer. @samp{%x} uses lower case and @samp{%X} uses upper case.
787
788 @item %c
789 Replace the specification with the character which is the value given.
790
791 @item %e
792 Replace the specification with the exponential notation for a floating
793 point number.
794
795 @item %f
796 Replace the specification with the decimal-point notation for a floating
797 point number.
798
799 @item %g
800 Replace the specification with notation for a floating point number,
801 using either exponential notation or decimal-point notation, whichever
802 is shorter.
803
804 @item %%
805 Replace the specification with a single @samp{%}. This format
806 specification is unusual in that it does not use a value. For example,
807 @code{(format "%% %d" 30)} returns @code{"% 30"}.
808 @end table
809
810 Any other format character results in an @samp{Invalid format
811 operation} error.
812
813 Here are several examples:
814
815 @example
816 @group
817 (format "The name of this buffer is %s." (buffer-name))
818 @result{} "The name of this buffer is strings.texi."
819
820 (format "The buffer object prints as %s." (current-buffer))
821 @result{} "The buffer object prints as strings.texi."
822
823 (format "The octal value of %d is %o,
824 and the hex value is %x." 18 18 18)
825 @result{} "The octal value of 18 is 22,
826 and the hex value is 12."
827 @end group
828 @end example
829
830 @cindex field width
831 @cindex padding
832 A specification can have a @dfn{width}, which is a decimal number
833 between the @samp{%} and the specification character. If the printed
834 representation of the object contains fewer characters than this
835 width, @code{format} extends it with padding. The width specifier is
836 ignored for the @samp{%%} specification. Any padding introduced by
837 the width specifier normally consists of spaces inserted on the left:
838
839 @example
840 (format "%5d is padded on the left with spaces" 123)
841 @result{} " 123 is padded on the left with spaces"
842 @end example
843
844 @noindent
845 If the width is too small, @code{format} does not truncate the
846 object's printed representation. Thus, you can use a width to specify
847 a minimum spacing between columns with no risk of losing information.
848 In the following three examples, @samp{%7s} specifies a minimum width
849 of 7. In the first case, the string inserted in place of @samp{%7s}
850 has only 3 letters, and needs 4 blank spaces as padding. In the
851 second case, the string @code{"specification"} is 13 letters wide but
852 is not truncated.
853
854 @example
855 @group
856 (format "The word `%7s' actually has %d letters in it."
857 "foo" (length "foo"))
858 @result{} "The word ` foo' actually has 3 letters in it."
859 (format "The word `%7s' actually has %d letters in it."
860 "specification" (length "specification"))
861 @result{} "The word `specification' actually has 13 letters in it."
862 @end group
863 @end example
864
865 @cindex flags in format specifications
866 Immediately after the @samp{%} and before the optional width
867 specifier, you can also put certain @dfn{flag characters}.
868
869 The flag @samp{+} inserts a plus sign before a positive number, so
870 that it always has a sign. A space character as flag inserts a space
871 before a positive number. (Otherwise, positive numbers start with the
872 first digit.) These flags are useful for ensuring that positive
873 numbers and negative numbers use the same number of columns. They are
874 ignored except for @samp{%d}, @samp{%e}, @samp{%f}, @samp{%g}, and if
875 both flags are used, @samp{+} takes precedence.
876
877 The flag @samp{#} specifies an ``alternate form'' which depends on
878 the format in use. For @samp{%o}, it ensures that the result begins
879 with a @samp{0}. For @samp{%x} and @samp{%X}, it prefixes the result
880 with @samp{0x} or @samp{0X}. For @samp{%e}, @samp{%f}, and @samp{%g},
881 the @samp{#} flag means include a decimal point even if the precision
882 is zero.
883
884 The flag @samp{-} causes the padding inserted by the width
885 specifier, if any, to be inserted on the right rather than the left.
886 The flag @samp{0} ensures that the padding consists of @samp{0}
887 characters instead of spaces, inserted on the left. These flags are
888 ignored for specification characters for which they do not make sense:
889 @samp{%s}, @samp{%S} and @samp{%c} accept the @samp{0} flag, but still
890 pad with @emph{spaces} on the left. If both @samp{-} and @samp{0} are
891 present and valid, @samp{-} takes precedence.
892
893 @example
894 @group
895 (format "%06d is padded on the left with zeros" 123)
896 @result{} "000123 is padded on the left with zeros"
897
898 (format "%-6d is padded on the right" 123)
899 @result{} "123 is padded on the right"
900
901 (format "The word `%-7s' actually has %d letters in it."
902 "foo" (length "foo"))
903 @result{} "The word `foo ' actually has 3 letters in it."
904 @end group
905 @end example
906
907 @cindex precision in format specifications
908 All the specification characters allow an optional @dfn{precision}
909 before the character (after the width, if present). The precision is
910 a decimal-point @samp{.} followed by a digit-string. For the
911 floating-point specifications (@samp{%e}, @samp{%f}, @samp{%g}), the
912 precision specifies how many decimal places to show; if zero, the
913 decimal-point itself is also omitted. For @samp{%s} and @samp{%S},
914 the precision truncates the string to the given width, so @samp{%.3s}
915 shows only the first three characters of the representation for
916 @var{object}. Precision has no effect for other specification
917 characters.
918
919 @node Case Conversion
920 @comment node-name, next, previous, up
921 @section Case Conversion in Lisp
922 @cindex upper case
923 @cindex lower case
924 @cindex character case
925 @cindex case conversion in Lisp
926
927 The character case functions change the case of single characters or
928 of the contents of strings. The functions normally convert only
929 alphabetic characters (the letters @samp{A} through @samp{Z} and
930 @samp{a} through @samp{z}, as well as non-@acronym{ASCII} letters); other
931 characters are not altered. You can specify a different case
932 conversion mapping by specifying a case table (@pxref{Case Tables}).
933
934 These functions do not modify the strings that are passed to them as
935 arguments.
936
937 The examples below use the characters @samp{X} and @samp{x} which have
938 @acronym{ASCII} codes 88 and 120 respectively.
939
940 @defun downcase string-or-char
941 This function converts a character or a string to lower case.
942
943 When the argument to @code{downcase} is a string, the function creates
944 and returns a new string in which each letter in the argument that is
945 upper case is converted to lower case. When the argument to
946 @code{downcase} is a character, @code{downcase} returns the
947 corresponding lower case character. This value is an integer. If the
948 original character is lower case, or is not a letter, then the value
949 equals the original character.
950
951 @example
952 (downcase "The cat in the hat")
953 @result{} "the cat in the hat"
954
955 (downcase ?X)
956 @result{} 120
957 @end example
958 @end defun
959
960 @defun upcase string-or-char
961 This function converts a character or a string to upper case.
962
963 When the argument to @code{upcase} is a string, the function creates
964 and returns a new string in which each letter in the argument that is
965 lower case is converted to upper case.
966
967 When the argument to @code{upcase} is a character, @code{upcase}
968 returns the corresponding upper case character. This value is an integer.
969 If the original character is upper case, or is not a letter, then the
970 value returned equals the original character.
971
972 @example
973 (upcase "The cat in the hat")
974 @result{} "THE CAT IN THE HAT"
975
976 (upcase ?x)
977 @result{} 88
978 @end example
979 @end defun
980
981 @defun capitalize string-or-char
982 @cindex capitalization
983 This function capitalizes strings or characters. If
984 @var{string-or-char} is a string, the function creates and returns a new
985 string, whose contents are a copy of @var{string-or-char} in which each
986 word has been capitalized. This means that the first character of each
987 word is converted to upper case, and the rest are converted to lower
988 case.
989
990 The definition of a word is any sequence of consecutive characters that
991 are assigned to the word constituent syntax class in the current syntax
992 table (@pxref{Syntax Class Table}).
993
994 When the argument to @code{capitalize} is a character, @code{capitalize}
995 has the same result as @code{upcase}.
996
997 @example
998 @group
999 (capitalize "The cat in the hat")
1000 @result{} "The Cat In The Hat"
1001 @end group
1002
1003 @group
1004 (capitalize "THE 77TH-HATTED CAT")
1005 @result{} "The 77th-Hatted Cat"
1006 @end group
1007
1008 @group
1009 (capitalize ?x)
1010 @result{} 88
1011 @end group
1012 @end example
1013 @end defun
1014
1015 @defun upcase-initials string-or-char
1016 If @var{string-or-char} is a string, this function capitalizes the
1017 initials of the words in @var{string-or-char}, without altering any
1018 letters other than the initials. It returns a new string whose
1019 contents are a copy of @var{string-or-char}, in which each word has
1020 had its initial letter converted to upper case.
1021
1022 The definition of a word is any sequence of consecutive characters that
1023 are assigned to the word constituent syntax class in the current syntax
1024 table (@pxref{Syntax Class Table}).
1025
1026 When the argument to @code{upcase-initials} is a character,
1027 @code{upcase-initials} has the same result as @code{upcase}.
1028
1029 @example
1030 @group
1031 (upcase-initials "The CAT in the hAt")
1032 @result{} "The CAT In The HAt"
1033 @end group
1034 @end example
1035 @end defun
1036
1037 @xref{Text Comparison}, for functions that compare strings; some of
1038 them ignore case differences, or can optionally ignore case differences.
1039
1040 @node Case Tables
1041 @section The Case Table
1042
1043 You can customize case conversion by installing a special @dfn{case
1044 table}. A case table specifies the mapping between upper case and lower
1045 case letters. It affects both the case conversion functions for Lisp
1046 objects (see the previous section) and those that apply to text in the
1047 buffer (@pxref{Case Changes}). Each buffer has a case table; there is
1048 also a standard case table which is used to initialize the case table
1049 of new buffers.
1050
1051 A case table is a char-table (@pxref{Char-Tables}) whose subtype is
1052 @code{case-table}. This char-table maps each character into the
1053 corresponding lower case character. It has three extra slots, which
1054 hold related tables:
1055
1056 @table @var
1057 @item upcase
1058 The upcase table maps each character into the corresponding upper
1059 case character.
1060 @item canonicalize
1061 The canonicalize table maps all of a set of case-related characters
1062 into a particular member of that set.
1063 @item equivalences
1064 The equivalences table maps each one of a set of case-related characters
1065 into the next character in that set.
1066 @end table
1067
1068 In simple cases, all you need to specify is the mapping to lower-case;
1069 the three related tables will be calculated automatically from that one.
1070
1071 For some languages, upper and lower case letters are not in one-to-one
1072 correspondence. There may be two different lower case letters with the
1073 same upper case equivalent. In these cases, you need to specify the
1074 maps for both lower case and upper case.
1075
1076 The extra table @var{canonicalize} maps each character to a canonical
1077 equivalent; any two characters that are related by case-conversion have
1078 the same canonical equivalent character. For example, since @samp{a}
1079 and @samp{A} are related by case-conversion, they should have the same
1080 canonical equivalent character (which should be either @samp{a} for both
1081 of them, or @samp{A} for both of them).
1082
1083 The extra table @var{equivalences} is a map that cyclically permutes
1084 each equivalence class (of characters with the same canonical
1085 equivalent). (For ordinary @acronym{ASCII}, this would map @samp{a} into
1086 @samp{A} and @samp{A} into @samp{a}, and likewise for each set of
1087 equivalent characters.)
1088
1089 When you construct a case table, you can provide @code{nil} for
1090 @var{canonicalize}; then Emacs fills in this slot from the lower case
1091 and upper case mappings. You can also provide @code{nil} for
1092 @var{equivalences}; then Emacs fills in this slot from
1093 @var{canonicalize}. In a case table that is actually in use, those
1094 components are non-@code{nil}. Do not try to specify @var{equivalences}
1095 without also specifying @var{canonicalize}.
1096
1097 Here are the functions for working with case tables:
1098
1099 @defun case-table-p object
1100 This predicate returns non-@code{nil} if @var{object} is a valid case
1101 table.
1102 @end defun
1103
1104 @defun set-standard-case-table table
1105 This function makes @var{table} the standard case table, so that it will
1106 be used in any buffers created subsequently.
1107 @end defun
1108
1109 @defun standard-case-table
1110 This returns the standard case table.
1111 @end defun
1112
1113 @defun current-case-table
1114 This function returns the current buffer's case table.
1115 @end defun
1116
1117 @defun set-case-table table
1118 This sets the current buffer's case table to @var{table}.
1119 @end defun
1120
1121 @defmac with-case-table table body@dots{}
1122 The @code{with-case-table} macro saves the current case table, makes
1123 @var{table} the current case table, evaluates the @var{body} forms,
1124 and finally restores the case table. The return value is the value of
1125 the last form in @var{body}. The case table is restored even in case
1126 of an abnormal exit via @code{throw} or error (@pxref{Nonlocal
1127 Exits}).
1128 @end defmac
1129
1130 Some language environments may modify the case conversions of
1131 @acronym{ASCII} characters; for example, in the Turkish language
1132 environment, the @acronym{ASCII} character @samp{I} is downcased into
1133 a Turkish ``dotless i''. This can interfere with code that requires
1134 ordinary ASCII case conversion, such as implementations of
1135 @acronym{ASCII}-based network protocols. In that case, use the
1136 @code{with-case-table} macro with the variable @var{ascii-case-table},
1137 which stores the unmodified case table for the @acronym{ASCII}
1138 character set.
1139
1140 @defvar ascii-case-table
1141 The case table for the @acronym{ASCII} character set. This should not be
1142 modified by any language environment settings.
1143 @end defvar
1144
1145 The following three functions are convenient subroutines for packages
1146 that define non-@acronym{ASCII} character sets. They modify the specified
1147 case table @var{case-table}; they also modify the standard syntax table.
1148 @xref{Syntax Tables}. Normally you would use these functions to change
1149 the standard case table.
1150
1151 @defun set-case-syntax-pair uc lc case-table
1152 This function specifies a pair of corresponding letters, one upper case
1153 and one lower case.
1154 @end defun
1155
1156 @defun set-case-syntax-delims l r case-table
1157 This function makes characters @var{l} and @var{r} a matching pair of
1158 case-invariant delimiters.
1159 @end defun
1160
1161 @defun set-case-syntax char syntax case-table
1162 This function makes @var{char} case-invariant, with syntax
1163 @var{syntax}.
1164 @end defun
1165
1166 @deffn Command describe-buffer-case-table
1167 This command displays a description of the contents of the current
1168 buffer's case table.
1169 @end deffn
1170
1171 @ignore
1172 arch-tag: 700b8e95-7aa5-4b52-9eb3-8f2e1ea152b4
1173 @end ignore