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 |
6554
|
31 @cindex escape sequence notation |
|
32 In double-quoted strings, the backslash character is used to introduce |
6556
|
33 @dfn{escape sequences} that reresent other characters. For example, |
6554
|
34 @samp{\n} embeds a newline character in a double-quoted string and |
|
35 @samp{\"} embeds a double quote character. |
3294
|
36 |
6554
|
37 In single-quoted strings, backslash is not a special character. |
|
38 |
|
39 Here is an example showing the difference |
3294
|
40 |
6554
|
41 @example |
6556
|
42 @group |
6554
|
43 toascii ("\n") |
6570
|
44 @result{} 10 |
6554
|
45 toascii ('\n') |
6570
|
46 @result{} [ 92 110 ] |
6556
|
47 @end group |
6554
|
48 @end example |
3294
|
49 |
6554
|
50 You may also insert a single quote character in a single-quoted string |
|
51 by using two single quote characters in succession. For example, |
|
52 |
|
53 @example |
|
54 'I can''t escape' |
6570
|
55 @result{} I can't escape |
6554
|
56 @end example |
3294
|
57 |
|
58 Here is a table of all the escape sequences used in Octave. They are |
|
59 the same as those used in the C programming language. |
|
60 |
|
61 @table @code |
|
62 @item \\ |
|
63 Represents a literal backslash, @samp{\}. |
|
64 |
|
65 @item \" |
|
66 Represents a literal double-quote character, @samp{"}. |
|
67 |
|
68 @item \' |
|
69 Represents a literal single-quote character, @samp{'}. |
|
70 |
3893
|
71 @item \0 |
4946
|
72 Represents the ``nul'' character, control-@@, ASCII code 0. |
3893
|
73 |
3294
|
74 @item \a |
|
75 Represents the ``alert'' character, control-g, ASCII code 7. |
|
76 |
|
77 @item \b |
|
78 Represents a backspace, control-h, ASCII code 8. |
|
79 |
|
80 @item \f |
|
81 Represents a formfeed, control-l, ASCII code 12. |
|
82 |
|
83 @item \n |
|
84 Represents a newline, control-j, ASCII code 10. |
|
85 |
|
86 @item \r |
|
87 Represents a carriage return, control-m, ASCII code 13. |
|
88 |
|
89 @item \t |
|
90 Represents a horizontal tab, control-i, ASCII code 9. |
|
91 |
|
92 @item \v |
|
93 Represents a vertical tab, control-k, ASCII code 11. |
|
94 |
|
95 @c We don't do octal or hex this way yet. |
|
96 @c |
|
97 @c @item \@var{nnn} |
|
98 @c Represents the octal value @var{nnn}, where @var{nnn} are one to three |
|
99 @c digits between 0 and 7. For example, the code for the ASCII ESC |
|
100 @c (escape) character is @samp{\033}.@refill |
|
101 @c |
|
102 @c @item \x@var{hh}@dots{} |
|
103 @c Represents the hexadecimal value @var{hh}, where @var{hh} are hexadecimal |
|
104 @c digits (@samp{0} through @samp{9} and either @samp{A} through @samp{F} or |
|
105 @c @samp{a} through @samp{f}). Like the same construct in @sc{ansi} C, |
|
106 @c the escape |
|
107 @c sequence continues until the first non-hexadecimal digit is seen. However, |
|
108 @c using more than two hexadecimal digits produces undefined results. (The |
|
109 @c @samp{\x} escape sequence is not allowed in @sc{posix} @code{awk}.)@refill |
|
110 @end table |
|
111 |
|
112 Strings may be concatenated using the notation for defining matrices. |
|
113 For example, the expression |
|
114 |
|
115 @example |
|
116 [ "foo" , "bar" , "baz" ] |
|
117 @end example |
|
118 |
|
119 @noindent |
|
120 produces the string whose contents are @samp{foobarbaz}. @xref{Numeric |
3402
|
121 Data Types}, for more information about creating matrices. |
3294
|
122 |
|
123 @menu |
|
124 * Creating Strings:: |
|
125 * Searching and Replacing:: |
|
126 * String Conversions:: |
|
127 * Character Class Functions:: |
|
128 @end menu |
|
129 |
4167
|
130 @node Creating Strings |
3294
|
131 @section Creating Strings |
|
132 |
3361
|
133 @DOCSTRING(blanks) |
3294
|
134 |
4358
|
135 @DOCSTRING(char) |
|
136 |
3361
|
137 @DOCSTRING(int2str) |
3294
|
138 |
3428
|
139 @DOCSTRING(com2str) |
|
140 |
3361
|
141 @DOCSTRING(strcat) |
3294
|
142 |
6502
|
143 @DOCSTRING(strvcat) |
|
144 |
|
145 @DOCSTRING(strtrunc) |
|
146 |
3361
|
147 @DOCSTRING(string_fill_char) |
3294
|
148 |
3361
|
149 @DOCSTRING(str2mat) |
3294
|
150 |
4535
|
151 @DOCSTRING(ischar) |
|
152 |
6502
|
153 @DOCSTRING(mat2str) |
|
154 |
|
155 @DOCSTRING(num2str) |
3294
|
156 |
4167
|
157 @node Searching and Replacing |
3294
|
158 @section Searching and Replacing |
|
159 |
3361
|
160 @DOCSTRING(deblank) |
3294
|
161 |
3361
|
162 @DOCSTRING(findstr) |
3294
|
163 |
3361
|
164 @DOCSTRING(index) |
3294
|
165 |
3361
|
166 @DOCSTRING(rindex) |
3294
|
167 |
6502
|
168 @DOCSTRING(strfind) |
|
169 |
|
170 @DOCSTRING(strmatch) |
|
171 |
|
172 @DOCSTRING(strtok) |
|
173 |
3361
|
174 @DOCSTRING(split) |
3294
|
175 |
3361
|
176 @DOCSTRING(strcmp) |
3294
|
177 |
6502
|
178 @DOCSTRING(strcmpi) |
|
179 |
|
180 @DOCSTRING(strncmp) |
|
181 |
|
182 @DOCSTRING(strncmpi) |
|
183 |
3361
|
184 @DOCSTRING(strrep) |
3294
|
185 |
3361
|
186 @DOCSTRING(substr) |
3294
|
187 |
5582
|
188 @DOCSTRING(regexp) |
|
189 |
|
190 @DOCSTRING(regexpi) |
|
191 |
6549
|
192 @DOCSTRING(regexprep) |
|
193 |
4167
|
194 @node String Conversions |
3294
|
195 @section String Conversions |
|
196 |
3361
|
197 @DOCSTRING(bin2dec) |
3294
|
198 |
3361
|
199 @DOCSTRING(dec2bin) |
3294
|
200 |
3361
|
201 @DOCSTRING(dec2hex) |
3294
|
202 |
3361
|
203 @DOCSTRING(hex2dec) |
3294
|
204 |
3920
|
205 @DOCSTRING(dec2base) |
|
206 |
|
207 @DOCSTRING(base2dec) |
|
208 |
|
209 @DOCSTRING(strjust) |
|
210 |
6502
|
211 @DOCSTRING(str2double) |
|
212 |
3361
|
213 @DOCSTRING(str2num) |
3294
|
214 |
3361
|
215 @DOCSTRING(toascii) |
3294
|
216 |
3361
|
217 @DOCSTRING(tolower) |
3294
|
218 |
3361
|
219 @DOCSTRING(toupper) |
3294
|
220 |
3428
|
221 @DOCSTRING(do_string_escapes) |
|
222 |
3361
|
223 @DOCSTRING(undo_string_escapes) |
3294
|
224 |
4167
|
225 @node Character Class Functions |
3294
|
226 @section Character Class Functions |
|
227 |
|
228 Octave also provides the following character class test functions |
|
229 patterned after the functions in the standard C library. They all |
|
230 operate on string arrays and return matrices of zeros and ones. |
|
231 Elements that are nonzero indicate that the condition was true for the |
|
232 corresponding character in the string array. For example, |
|
233 |
|
234 @example |
|
235 @group |
|
236 isalpha ("!Q@@WERT^Y&") |
|
237 @result{} [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ] |
|
238 @end group |
|
239 @end example |
|
240 |
3361
|
241 @DOCSTRING(isalnum) |
3294
|
242 |
3361
|
243 @DOCSTRING(isalpha) |
|
244 |
|
245 @DOCSTRING(isascii) |
3294
|
246 |
3361
|
247 @DOCSTRING(iscntrl) |
3294
|
248 |
3361
|
249 @DOCSTRING(isdigit) |
3294
|
250 |
3361
|
251 @DOCSTRING(isgraph) |
3294
|
252 |
6549
|
253 @DOCSTRING(isletter) |
|
254 |
3361
|
255 @DOCSTRING(islower) |
3294
|
256 |
3361
|
257 @DOCSTRING(isprint) |
3294
|
258 |
3361
|
259 @DOCSTRING(ispunct) |
3294
|
260 |
3361
|
261 @DOCSTRING(isspace) |
3294
|
262 |
3361
|
263 @DOCSTRING(isupper) |
3294
|
264 |
3361
|
265 @DOCSTRING(isxdigit) |