3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node Strings |
3294
|
6 @chapter Strings |
|
7 @cindex strings |
|
8 @cindex character strings |
|
9 @opindex " |
|
10 @opindex ' |
|
11 |
|
12 A @dfn{string constant} consists of a sequence of characters enclosed in |
|
13 either double-quote or single-quote marks. For example, both of the |
|
14 following expressions |
|
15 |
|
16 @example |
|
17 @group |
|
18 "parrot" |
|
19 'parrot' |
|
20 @end group |
|
21 @end example |
|
22 |
|
23 @noindent |
|
24 represent the string whose contents are @samp{parrot}. Strings in |
|
25 Octave can be of any length. |
|
26 |
|
27 Since the single-quote mark is also used for the transpose operator |
|
28 (@pxref{Arithmetic Ops}) but double-quote marks have no other purpose in |
|
29 Octave, it is best to use double-quote marks to denote strings. |
|
30 |
|
31 @c XXX FIXME XXX -- this is probably pretty confusing. |
|
32 |
|
33 @cindex escape sequence notation |
|
34 Some characters cannot be included literally in a string constant. You |
|
35 represent them instead with @dfn{escape sequences}, which are character |
|
36 sequences beginning with a backslash (@samp{\}). |
|
37 |
|
38 One use of an escape sequence is to include a double-quote |
|
39 (single-quote) character in a string constant that has been defined |
|
40 using double-quote (single-quote) marks. Since a plain double-quote |
|
41 would end the string, you must use @samp{\"} to represent a single |
|
42 double-quote character as a part of the string. The backslash character |
|
43 itself is another character that cannot be included normally. You must |
|
44 write @samp{\\} to put one backslash in the string. Thus, the string |
|
45 whose contents are the two characters @samp{"\} may be written |
|
46 @code{"\"\\"} or @code{'"\\'}. Similarly, the string whose contents are |
|
47 the two characters @samp{'\} may be written @code{'\'\\'} or @code{"'\\"}. |
|
48 |
|
49 Another use of backslash is to represent unprintable characters |
|
50 such as newline. While there is nothing to stop you from writing most |
|
51 of these characters directly in a string constant, they may look ugly. |
|
52 |
|
53 Here is a table of all the escape sequences used in Octave. They are |
|
54 the same as those used in the C programming language. |
|
55 |
|
56 @table @code |
|
57 @item \\ |
|
58 Represents a literal backslash, @samp{\}. |
|
59 |
|
60 @item \" |
|
61 Represents a literal double-quote character, @samp{"}. |
|
62 |
|
63 @item \' |
|
64 Represents a literal single-quote character, @samp{'}. |
|
65 |
3893
|
66 @item \0 |
|
67 Represents the ``nul'' character, control-@, ASCII code 0. |
|
68 |
3294
|
69 @item \a |
|
70 Represents the ``alert'' character, control-g, ASCII code 7. |
|
71 |
|
72 @item \b |
|
73 Represents a backspace, control-h, ASCII code 8. |
|
74 |
|
75 @item \f |
|
76 Represents a formfeed, control-l, ASCII code 12. |
|
77 |
|
78 @item \n |
|
79 Represents a newline, control-j, ASCII code 10. |
|
80 |
|
81 @item \r |
|
82 Represents a carriage return, control-m, ASCII code 13. |
|
83 |
|
84 @item \t |
|
85 Represents a horizontal tab, control-i, ASCII code 9. |
|
86 |
|
87 @item \v |
|
88 Represents a vertical tab, control-k, ASCII code 11. |
|
89 |
|
90 @c We don't do octal or hex this way yet. |
|
91 @c |
|
92 @c @item \@var{nnn} |
|
93 @c Represents the octal value @var{nnn}, where @var{nnn} are one to three |
|
94 @c digits between 0 and 7. For example, the code for the ASCII ESC |
|
95 @c (escape) character is @samp{\033}.@refill |
|
96 @c |
|
97 @c @item \x@var{hh}@dots{} |
|
98 @c Represents the hexadecimal value @var{hh}, where @var{hh} are hexadecimal |
|
99 @c digits (@samp{0} through @samp{9} and either @samp{A} through @samp{F} or |
|
100 @c @samp{a} through @samp{f}). Like the same construct in @sc{ansi} C, |
|
101 @c the escape |
|
102 @c sequence continues until the first non-hexadecimal digit is seen. However, |
|
103 @c using more than two hexadecimal digits produces undefined results. (The |
|
104 @c @samp{\x} escape sequence is not allowed in @sc{posix} @code{awk}.)@refill |
|
105 @end table |
|
106 |
|
107 Strings may be concatenated using the notation for defining matrices. |
|
108 For example, the expression |
|
109 |
|
110 @example |
|
111 [ "foo" , "bar" , "baz" ] |
|
112 @end example |
|
113 |
|
114 @noindent |
|
115 produces the string whose contents are @samp{foobarbaz}. @xref{Numeric |
3402
|
116 Data Types}, for more information about creating matrices. |
3294
|
117 |
|
118 @menu |
|
119 * Creating Strings:: |
|
120 * Searching and Replacing:: |
|
121 * String Conversions:: |
|
122 * Character Class Functions:: |
|
123 @end menu |
|
124 |
4167
|
125 @node Creating Strings |
3294
|
126 @section Creating Strings |
|
127 |
3361
|
128 @DOCSTRING(blanks) |
3294
|
129 |
3361
|
130 @DOCSTRING(int2str) |
3294
|
131 |
3428
|
132 @DOCSTRING(com2str) |
|
133 |
3361
|
134 @DOCSTRING(setstr) |
3294
|
135 |
3361
|
136 @DOCSTRING(strcat) |
3294
|
137 |
3361
|
138 @DOCSTRING(string_fill_char) |
3294
|
139 |
3361
|
140 @DOCSTRING(str2mat) |
3294
|
141 |
3361
|
142 @DOCSTRING(isstr) |
3294
|
143 |
4167
|
144 @node Searching and Replacing |
3294
|
145 @section Searching and Replacing |
|
146 |
3361
|
147 @DOCSTRING(deblank) |
3294
|
148 |
3361
|
149 @DOCSTRING(findstr) |
3294
|
150 |
3361
|
151 @DOCSTRING(index) |
3294
|
152 |
3361
|
153 @DOCSTRING(rindex) |
3294
|
154 |
3361
|
155 @DOCSTRING(split) |
3294
|
156 |
3361
|
157 @DOCSTRING(strcmp) |
3294
|
158 |
3361
|
159 @DOCSTRING(strrep) |
3294
|
160 |
3361
|
161 @DOCSTRING(substr) |
3294
|
162 |
4167
|
163 @node String Conversions |
3294
|
164 @section String Conversions |
|
165 |
3361
|
166 @DOCSTRING(bin2dec) |
3294
|
167 |
3361
|
168 @DOCSTRING(dec2bin) |
3294
|
169 |
3361
|
170 @DOCSTRING(dec2hex) |
3294
|
171 |
3361
|
172 @DOCSTRING(hex2dec) |
3294
|
173 |
3920
|
174 @DOCSTRING(dec2base) |
|
175 |
|
176 @DOCSTRING(base2dec) |
|
177 |
|
178 @DOCSTRING(strjust) |
|
179 |
3361
|
180 @DOCSTRING(str2num) |
3294
|
181 |
3361
|
182 @DOCSTRING(toascii) |
3294
|
183 |
3361
|
184 @DOCSTRING(tolower) |
3294
|
185 |
3361
|
186 @DOCSTRING(toupper) |
3294
|
187 |
3428
|
188 @DOCSTRING(do_string_escapes) |
|
189 |
3361
|
190 @DOCSTRING(undo_string_escapes) |
3294
|
191 |
3361
|
192 @DOCSTRING(implicit_num_to_str_ok) |
3294
|
193 |
3361
|
194 @DOCSTRING(implicit_str_to_num_ok) |
3294
|
195 |
3428
|
196 @DOCSTRING(warn_single_quote_string) |
|
197 |
4167
|
198 @node Character Class Functions |
3294
|
199 @section Character Class Functions |
|
200 |
|
201 Octave also provides the following character class test functions |
|
202 patterned after the functions in the standard C library. They all |
|
203 operate on string arrays and return matrices of zeros and ones. |
|
204 Elements that are nonzero indicate that the condition was true for the |
|
205 corresponding character in the string array. For example, |
|
206 |
|
207 @example |
|
208 @group |
|
209 isalpha ("!Q@@WERT^Y&") |
|
210 @result{} [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ] |
|
211 @end group |
|
212 @end example |
|
213 |
3361
|
214 @DOCSTRING(isalnum) |
3294
|
215 |
3361
|
216 @DOCSTRING(isalpha) |
|
217 |
|
218 @DOCSTRING(isascii) |
3294
|
219 |
3361
|
220 @DOCSTRING(iscntrl) |
3294
|
221 |
3361
|
222 @DOCSTRING(isdigit) |
3294
|
223 |
3361
|
224 @DOCSTRING(isgraph) |
3294
|
225 |
3361
|
226 @DOCSTRING(islower) |
3294
|
227 |
3361
|
228 @DOCSTRING(isprint) |
3294
|
229 |
3361
|
230 @DOCSTRING(ispunct) |
3294
|
231 |
3361
|
232 @DOCSTRING(isspace) |
3294
|
233 |
3361
|
234 @DOCSTRING(isupper) |
3294
|
235 |
3361
|
236 @DOCSTRING(isxdigit) |