Mercurial > octave
annotate doc/interpreter/strings.txi @ 28212:d052c11d47c6
Add new functions startsWith and endsWith (bug #57041).
* scripts/strings: Add startsWith.m and endsWith.m.
* scripts/strings/module.mk: Add references to new files.
* doc/interpreter/strings.txi: Add docstrings to manual.
* NEWS: Add to list of new functions.
* scripts/help/__unimplemented__.m: Remove the two functions.
author | Abdallah Elshamy <abdallah.k.elshamy@gmail.com> |
---|---|
date | Sun, 29 Mar 2020 22:15:32 +0200 |
parents | f26b13c80e45 |
children | 888e79a46a85 |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25412
diff
changeset
|
1 @c Copyright (C) 1996-2019 John W. Eaton |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24474
diff
changeset
|
5 @c Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
6 @c under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24474
diff
changeset
|
7 @c the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 @c (at your option) any later version. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
9 @c |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
10 @c Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
11 @c WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 @c GNU General Public License for more details. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
14 @c |
7018 | 15 @c You should have received a copy of the GNU General Public License |
16 @c along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24474
diff
changeset
|
17 @c <https://www.gnu.org/licenses/>. |
3294 | 18 |
4167 | 19 @node Strings |
3294 | 20 @chapter Strings |
21 @cindex strings | |
22 @cindex character strings | |
23 @opindex " | |
24550
966c3283a164
doc: Fix appearance of '.' and "'" in Operator Index (bug #52813).
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
24 @opindex @code{'} |
3294 | 25 |
26 A @dfn{string constant} consists of a sequence of characters enclosed in | |
27 either double-quote or single-quote marks. For example, both of the | |
28 following expressions | |
29 | |
30 @example | |
31 @group | |
32 "parrot" | |
33 'parrot' | |
34 @end group | |
35 @end example | |
36 | |
37 @noindent | |
38 represent the string whose contents are @samp{parrot}. Strings in | |
39 Octave can be of any length. | |
40 | |
41 Since the single-quote mark is also used for the transpose operator | |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
19956
diff
changeset
|
42 (@pxref{Arithmetic Ops}) but double-quote marks have no other purpose in |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
19956
diff
changeset
|
43 Octave, it is best to use double-quote marks to denote strings. |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
44 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
45 Strings can be concatenated using the notation for defining matrices. For |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
46 example, the expression |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
47 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
48 @example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
49 [ "foo" , "bar" , "baz" ] |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
50 @end example |
3294 | 51 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
52 @noindent |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
53 produces the string whose contents are @samp{foobarbaz}. @xref{Numeric Data |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
54 Types}, for more information about creating matrices. |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
55 |
25412
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
56 While strings can in principal store arbitrary content, most functions expect |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
57 them to be UTF-8 encoded Unicode strings. |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
58 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
59 @menu |
11255
d682cd6669ac
Update info-based documentation menus to include new nodes.
Rik <octave@nomad.inbox5.com>
parents:
11143
diff
changeset
|
60 * Escape Sequences in String Constants:: |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
61 * Character Arrays:: |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
62 * Creating Strings:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
63 * Comparing Strings:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
64 * Manipulating Strings:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
65 * String Conversions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
66 * Character Class Functions:: |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
67 @end menu |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
68 |
11257
8d4c57258523
fix some menu problems in the manual
John W. Eaton <jwe@octave.org>
parents:
11255
diff
changeset
|
69 @node Escape Sequences in String Constants |
8d4c57258523
fix some menu problems in the manual
John W. Eaton <jwe@octave.org>
parents:
11255
diff
changeset
|
70 @section Escape Sequences in String Constants |
6554 | 71 @cindex escape sequence notation |
72 In double-quoted strings, the backslash character is used to introduce | |
6623 | 73 @dfn{escape sequences} that represent other characters. For example, |
6554 | 74 @samp{\n} embeds a newline character in a double-quoted string and |
14528
c67822beb7a4
maint: Backed out changeset 8c988dabbe8e
Rik <octave@nomad.inbox5.com>
parents:
14527
diff
changeset
|
75 @samp{\"} embeds a double quote character. In single-quoted strings, backslash |
c67822beb7a4
maint: Backed out changeset 8c988dabbe8e
Rik <octave@nomad.inbox5.com>
parents:
14527
diff
changeset
|
76 is not a special character. Here is an example showing the difference: |
3294 | 77 |
6554 | 78 @example |
6556 | 79 @group |
24620 | 80 double ("\n") |
6570 | 81 @result{} 10 |
24620 | 82 double ('\n') |
6570 | 83 @result{} [ 92 110 ] |
6556 | 84 @end group |
6554 | 85 @end example |
3294 | 86 |
14528
c67822beb7a4
maint: Backed out changeset 8c988dabbe8e
Rik <octave@nomad.inbox5.com>
parents:
14527
diff
changeset
|
87 Here is a table of all the escape sequences used in Octave (within |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
88 double quoted strings). They are the same as those used in the C |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
89 programming language. |
3294 | 90 |
91 @table @code | |
92 @item \\ | |
93 Represents a literal backslash, @samp{\}. | |
94 | |
95 @item \" | |
96 Represents a literal double-quote character, @samp{"}. | |
97 | |
98 @item \' | |
99 Represents a literal single-quote character, @samp{'}. | |
100 | |
3893 | 101 @item \0 |
16826
a4969508008e
doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
102 Represents the null character, control-@@, ASCII code 0. |
3893 | 103 |
3294 | 104 @item \a |
105 Represents the ``alert'' character, control-g, ASCII code 7. | |
106 | |
107 @item \b | |
108 Represents a backspace, control-h, ASCII code 8. | |
109 | |
110 @item \f | |
111 Represents a formfeed, control-l, ASCII code 12. | |
112 | |
113 @item \n | |
114 Represents a newline, control-j, ASCII code 10. | |
115 | |
116 @item \r | |
117 Represents a carriage return, control-m, ASCII code 13. | |
118 | |
119 @item \t | |
120 Represents a horizontal tab, control-i, ASCII code 9. | |
121 | |
122 @item \v | |
123 Represents a vertical tab, control-k, ASCII code 11. | |
124 | |
17244
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
125 @item \@var{nnn} |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
126 Represents the octal value @var{nnn}, where @var{nnn} are one to three |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
127 digits between 0 and 7. For example, the code for the ASCII ESC |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
128 (escape) character is @samp{\033}. |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
129 |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
130 @item \x@var{hh}@dots{} |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
131 Represents the hexadecimal value @var{hh}, where @var{hh} are hexadecimal |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
132 digits (@samp{0} through @samp{9} and either @samp{A} through @samp{F} or |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
133 @samp{a} through @samp{f}). Like the same construct in @sc{ansi} C, |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
134 the escape sequence continues until the first non-hexadecimal digit is seen. |
9de751a10910
Add support for hexadecimal escape sequences in double-quoted strings (bug #39775)
Mike Miller <mtmiller@ieee.org>
parents:
17152
diff
changeset
|
135 However, using more than two hexadecimal digits produces undefined results. |
3294 | 136 @end table |
137 | |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
138 In a single-quoted string there is only one escape sequence: you may insert a |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
139 single quote character using two single quote characters in succession. For |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
140 example, |
3294 | 141 |
142 @example | |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
143 @group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
144 'I can''t escape' |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
145 @result{} I can't escape |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
146 @end group |
3294 | 147 @end example |
148 | |
14528
c67822beb7a4
maint: Backed out changeset 8c988dabbe8e
Rik <octave@nomad.inbox5.com>
parents:
14527
diff
changeset
|
149 In scripts the two different string types can be distinguished if necessary |
13943
5820f8ce683e
doc: Add undocumented functions to manual for 3.6.0 release
Rik <octave@nomad.inbox5.com>
parents:
12581
diff
changeset
|
150 by using @code{is_dq_string} and @code{is_sq_string}. |
5820f8ce683e
doc: Add undocumented functions to manual for 3.6.0 release
Rik <octave@nomad.inbox5.com>
parents:
12581
diff
changeset
|
151 |
5820f8ce683e
doc: Add undocumented functions to manual for 3.6.0 release
Rik <octave@nomad.inbox5.com>
parents:
12581
diff
changeset
|
152 @DOCSTRING(is_dq_string) |
5820f8ce683e
doc: Add undocumented functions to manual for 3.6.0 release
Rik <octave@nomad.inbox5.com>
parents:
12581
diff
changeset
|
153 |
5820f8ce683e
doc: Add undocumented functions to manual for 3.6.0 release
Rik <octave@nomad.inbox5.com>
parents:
12581
diff
changeset
|
154 @DOCSTRING(is_sq_string) |
3294 | 155 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
156 @node Character Arrays |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
157 @section Character Arrays |
3294 | 158 |
6623 | 159 The string representation used by Octave is an array of characters, so |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
160 internally the string @nospell{@qcode{"dddddddddd"}} is actually a row vector |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
161 of length 10 containing the value 100 in all places (100 is the ASCII code of |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
162 @qcode{"d"}). This lends itself to the obvious generalization to character |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
163 matrices. Using a matrix of characters, it is possible to represent a |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
164 collection of same-length strings in one variable. The convention used in |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
165 Octave is that each row in a character matrix is a separate string, but letting |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
166 each column represent a string is equally possible. |
6623 | 167 |
168 The easiest way to create a character matrix is to put several strings | |
169 together into a matrix. | |
170 | |
171 @example | |
172 collection = [ "String #1"; "String #2" ]; | |
173 @end example | |
174 | |
175 @noindent | |
176 This creates a 2-by-9 character matrix. | |
177 | |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
178 The function @code{ischar} can be used to test if an object is a character |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
179 matrix. |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
180 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
181 @DOCSTRING(ischar) |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
182 |
24474
0b65949870e3
isstring.m: Add new function for identifying string arrays.
Rik <rik@octave.org>
parents:
24460
diff
changeset
|
183 @DOCSTRING(isstring) |
0b65949870e3
isstring.m: Add new function for identifying string arrays.
Rik <rik@octave.org>
parents:
24460
diff
changeset
|
184 |
25003
2365c2661b3c
doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24620
diff
changeset
|
185 To test if an object is a string (i.e., a @nospell{1xN} row vector of |
2365c2661b3c
doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24620
diff
changeset
|
186 characters and not a character matrix) you can use the @code{ischar} function |
2365c2661b3c
doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24620
diff
changeset
|
187 in combination with the @code{isrow} function as in the following example: |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
188 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
189 @example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
190 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
191 ischar (collection) |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
192 @result{} 1 |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
193 |
24474
0b65949870e3
isstring.m: Add new function for identifying string arrays.
Rik <rik@octave.org>
parents:
24460
diff
changeset
|
194 ischar (collection) && isrow (collection) |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
195 @result{} 0 |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
196 |
24474
0b65949870e3
isstring.m: Add new function for identifying string arrays.
Rik <rik@octave.org>
parents:
24460
diff
changeset
|
197 ischar ("my string") && isrow ("my string") |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
198 @result{} 1 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
199 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
200 @end example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
201 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
202 One relevant question is, what happens when a character matrix is |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
203 created from strings of different length. The answer is that Octave |
6623 | 204 puts blank characters at the end of strings shorter than the longest |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
205 string. It is possible to use a different character than the |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
206 blank character using the @code{string_fill_char} function. |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
207 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
208 @DOCSTRING(string_fill_char) |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
209 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
210 This shows a problem with character matrices. It simply isn't possible to |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
211 represent strings of different lengths. The solution is to use a cell array of |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
212 strings, which is described in @ref{Cell Arrays of Strings}. |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
213 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
214 @node Creating Strings |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
215 @section Creating Strings |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
216 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
217 The easiest way to create a string is, as illustrated in the introduction, |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
218 to enclose a text in double-quotes or single-quotes. It is however |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
219 possible to create a string without actually writing a text. The |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
220 function @code{blanks} creates a string of a given length consisting |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
221 only of blank characters (ASCII code 32). |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
222 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
223 @DOCSTRING(blanks) |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
224 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
225 @menu |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
226 * Concatenating Strings:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
227 * Converting Numerical Data to Strings:: |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
228 @end menu |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
229 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
230 @node Concatenating Strings |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
231 @subsection Concatenating Strings |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
232 |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
233 Strings can be concatenated using matrix notation |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
234 (@pxref{Strings}, @ref{Character Arrays}) which is often the most natural |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
235 method. For example: |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
236 |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
237 @example |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
238 @group |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
239 fullname = [fname ".txt"]; |
17003
c6a39f7f193d
doc: Use '@@' to produce '@' in strings.txi
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
240 email = ["<" user "@@" domain ">"]; |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
241 @end group |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
242 @end example |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
243 |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
244 @noindent |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
245 In each case it is easy to see what the final string will look like. This |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
246 method is also the most efficient. When using matrix concatenation the parser |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
247 immediately begins joining the strings without having to process |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
248 the overhead of a function call and the input validation of the associated |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
249 function. |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
250 |
27141
f26b13c80e45
Implement new function "newline" for Matlab compatibility (bug #56419).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
26575
diff
changeset
|
251 The @code{newline} function can be used to join strings such that they appear |
f26b13c80e45
Implement new function "newline" for Matlab compatibility (bug #56419).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
26575
diff
changeset
|
252 as multiple lines of text when displayed. |
f26b13c80e45
Implement new function "newline" for Matlab compatibility (bug #56419).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
26575
diff
changeset
|
253 |
f26b13c80e45
Implement new function "newline" for Matlab compatibility (bug #56419).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
26575
diff
changeset
|
254 @DOCSTRING(newline) |
f26b13c80e45
Implement new function "newline" for Matlab compatibility (bug #56419).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
26575
diff
changeset
|
255 |
f26b13c80e45
Implement new function "newline" for Matlab compatibility (bug #56419).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
26575
diff
changeset
|
256 In addition, there are several other functions for concatenating string |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
257 objects which can be useful in specific circumstances: @code{char}, |
17097
e7a059a9a644
doc: Use XREF as anchor prefix in documentation for clearer results in Info viewer.
Rik <rik@octave.org>
parents:
17003
diff
changeset
|
258 @code{strvcat}, @code{strcat}, and @code{cstrcat}. Finally, the general |
e7a059a9a644
doc: Use XREF as anchor prefix in documentation for clearer results in Info viewer.
Rik <rik@octave.org>
parents:
17003
diff
changeset
|
259 purpose concatenation functions can be used: see @ref{XREFcat,,cat}, |
e7a059a9a644
doc: Use XREF as anchor prefix in documentation for clearer results in Info viewer.
Rik <rik@octave.org>
parents:
17003
diff
changeset
|
260 @ref{XREFhorzcat,,horzcat}, and @ref{XREFvertcat,,vertcat}. |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
261 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
262 @itemize @bullet |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
263 @item All string concatenation functions except @code{cstrcat} |
26575
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
264 convert numerical input into character data by taking the corresponding UTF-8 |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
265 character for each element (or multi-byte sequence), as in the following |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
266 example: |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
267 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
268 @example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
269 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
270 char ([98, 97, 110, 97, 110, 97]) |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
271 @result{} banana |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
272 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
273 @end example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
274 |
26575
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
275 For conversion between locale encodings and UTF-8, see |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
276 @ref{XREFunicode2native,,unicode2native} and |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
277 @ref{XREFnative2unicode,,native2unicode}. |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
278 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
279 @item |
8372
8dff9cba15fe
move str2mat to deprecated and make it a simple wrapper around char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8351
diff
changeset
|
280 @code{char} and @code{strvcat} |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
281 concatenate vertically, while @code{strcat} and @code{cstrcat} concatenate |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
282 horizontally. For example: |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
283 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
284 @example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
285 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
286 char ("an apple", "two pears") |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
287 @result{} an apple |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
288 two pears |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
289 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
290 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
291 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
292 strcat ("oc", "tave", " is", " good", " for you") |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
293 @result{} octave is good for you |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
294 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
295 @end example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
296 |
8372
8dff9cba15fe
move str2mat to deprecated and make it a simple wrapper around char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8351
diff
changeset
|
297 @item @code{char} generates an empty row in the output |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
298 for each empty string in the input. @code{strvcat}, on the other hand, |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
299 eliminates empty strings. |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
300 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
301 @example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
302 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
303 char ("orange", "green", "", "red") |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
304 @result{} orange |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
305 green |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
306 |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
307 red |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
308 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
309 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
310 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
311 strvcat ("orange", "green", "", "red") |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
312 @result{} orange |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
313 green |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
314 red |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
315 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
316 @end example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
317 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
318 @item All string concatenation functions except @code{cstrcat} also accept cell |
8372
8dff9cba15fe
move str2mat to deprecated and make it a simple wrapper around char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8351
diff
changeset
|
319 array data (@pxref{Cell Arrays}). @code{char} and |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
320 @code{strvcat} convert cell arrays into character arrays, while @code{strcat} |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
321 concatenates within the cells of the cell arrays: |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
322 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
323 @example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
324 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
325 char (@{"red", "green", "", "blue"@}) |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
326 @result{} red |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
327 green |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
328 |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
329 blue |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
330 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
331 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
332 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
333 strcat (@{"abc"; "ghi"@}, @{"def"; "jkl"@}) |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
334 @result{} |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
335 @{ |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
336 [1,1] = abcdef |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
337 [2,1] = ghijkl |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
338 @} |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
339 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
340 @end example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
341 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
342 @item @code{strcat} removes trailing white space in the arguments (except |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
343 within cell arrays), while @code{cstrcat} leaves white space untouched. Both |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
344 kinds of behavior can be useful as can be seen in the examples: |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
345 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
346 @example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
347 @group |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
348 strcat (["dir1";"directory2"], ["/";"/"], ["file1";"file2"]) |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17244
diff
changeset
|
349 @result{} dir1/file1 |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
350 directory2/file2 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
351 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
352 @group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
353 |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
354 cstrcat (["thirteen apples"; "a banana"], [" 5$";" 1$"]) |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
355 @result{} thirteen apples 5$ |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
356 a banana 1$ |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
357 @end group |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
358 @end example |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
359 |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
360 Note that in the above example for @code{cstrcat}, the white space originates |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
361 from the internal representation of the strings in a string array |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
362 (@pxref{Character Arrays}). |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
363 @end itemize |
6623 | 364 |
4358 | 365 @DOCSTRING(char) |
366 | |
6502 | 367 @DOCSTRING(strvcat) |
368 | |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
369 @DOCSTRING(strcat) |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
370 |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7984
diff
changeset
|
371 @DOCSTRING(cstrcat) |
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7984
diff
changeset
|
372 |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
373 @node Converting Numerical Data to Strings |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
374 @subsection Converting Numerical Data to Strings |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
375 Apart from the string concatenation functions (@pxref{Concatenating Strings}) |
26575
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
376 which cast numerical data to the corresponding UTF-8 encoded characters, there |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
377 are several functions that format numerical data as strings. @code{mat2str} |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
378 and @code{num2str} convert real or complex matrices, while @code{int2str} |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
379 converts integer matrices. @code{int2str} takes the real part of complex |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
380 values and round fractional values to integer. A more flexible way to format |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
381 numerical data as strings is the @code{sprintf} function |
3e7fa894731a
documentation: Replace ASCII with UTF-8 where it is more correct.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
382 (@pxref{Formatted Output}, @ref{XREFsprintf,,sprintf}). |
4535 | 383 |
6502 | 384 @DOCSTRING(mat2str) |
385 | |
386 @DOCSTRING(num2str) | |
3294 | 387 |
6623 | 388 @DOCSTRING(int2str) |
389 | |
390 @node Comparing Strings | |
391 @section Comparing Strings | |
392 | |
8828 | 393 Since a string is a character array, comparisons between strings work |
394 element by element as the following example shows: | |
6623 | 395 |
396 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
397 @group |
6623 | 398 GNU = "GNU's Not UNIX"; |
399 spaces = (GNU == " ") | |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
400 @result{} spaces = |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
401 0 0 0 0 0 1 0 0 0 1 0 0 0 0 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
402 @end group |
6623 | 403 @end example |
404 | |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
405 @noindent To determine if two strings are identical it is necessary to use the |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
406 @code{strcmp} function. It compares complete strings and is case |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
407 sensitive. @code{strncmp} compares only the first @code{N} characters (with |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
408 @code{N} given as a parameter). @code{strcmpi} and @code{strncmpi} are the |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
409 corresponding functions for case-insensitive comparison. |
6623 | 410 |
411 @DOCSTRING(strcmp) | |
412 | |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
413 @DOCSTRING(strncmp) |
6623 | 414 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
415 @DOCSTRING(strcmpi) |
6623 | 416 |
417 @DOCSTRING(strncmpi) | |
418 | |
28212
d052c11d47c6
Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
27141
diff
changeset
|
419 @DOCSTRING(startsWith) |
d052c11d47c6
Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
27141
diff
changeset
|
420 |
d052c11d47c6
Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
27141
diff
changeset
|
421 @DOCSTRING(endsWith) |
d052c11d47c6
Add new functions startsWith and endsWith (bug #57041).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
27141
diff
changeset
|
422 |
6623 | 423 @node Manipulating Strings |
424 @section Manipulating Strings | |
425 | |
426 Octave supports a wide range of functions for manipulating strings. | |
427 Since a string is just a matrix, simple manipulations can be accomplished | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
428 using standard operators. The following example shows how to replace |
6623 | 429 all blank characters with underscores. |
430 | |
431 @example | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
432 @group |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
433 quote = ... |
7081 | 434 "First things first, but not necessarily in that order"; |
6623 | 435 quote( quote == " " ) = "_" |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
436 @result{} quote = |
7081 | 437 First_things_first,_but_not_necessarily_in_that_order |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
438 @end group |
6623 | 439 @end example |
440 | |
441 For more complex manipulations, such as searching, replacing, and | |
7001 | 442 general regular expressions, the following functions come with Octave. |
3294 | 443 |
3361 | 444 @DOCSTRING(deblank) |
3294 | 445 |
8817
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8442
diff
changeset
|
446 @DOCSTRING(strtrim) |
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8442
diff
changeset
|
447 |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
448 @DOCSTRING(strtrunc) |
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
449 |
3361 | 450 @DOCSTRING(findstr) |
3294 | 451 |
8817
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8442
diff
changeset
|
452 @DOCSTRING(strchr) |
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8442
diff
changeset
|
453 |
3361 | 454 @DOCSTRING(index) |
3294 | 455 |
3361 | 456 @DOCSTRING(rindex) |
3294 | 457 |
6502 | 458 @DOCSTRING(strfind) |
459 | |
16408 | 460 @DOCSTRING(strjoin) |
461 | |
19956
55c2d33e6773
restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
462 @DOCSTRING(strmatch) |
55c2d33e6773
restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
463 |
6502 | 464 @DOCSTRING(strtok) |
465 | |
8879
1beaaffb3c1f
document strsplit, not split
John W. Eaton <jwe@octave.org>
parents:
8828
diff
changeset
|
466 @DOCSTRING(strsplit) |
3294 | 467 |
16725
57bf164f1608
Add ostrsplit() to the manual.
Ben Abbott <bpabbott@mac.com>
parents:
16408
diff
changeset
|
468 @DOCSTRING(ostrsplit) |
57bf164f1608
Add ostrsplit() to the manual.
Ben Abbott <bpabbott@mac.com>
parents:
16408
diff
changeset
|
469 |
11143
195cffc2d0a3
strings.txi: @DOCSTRING additions.
Ben Abbott <bpabbott@mac.com>
parents:
11103
diff
changeset
|
470 @DOCSTRING(strread) |
195cffc2d0a3
strings.txi: @DOCSTRING additions.
Ben Abbott <bpabbott@mac.com>
parents:
11103
diff
changeset
|
471 |
3361 | 472 @DOCSTRING(strrep) |
3294 | 473 |
24460
892f7f096ffb
erase.m: New string function for deleting substrings with a string.
Sahil Yadav
parents:
23220
diff
changeset
|
474 @DOCSTRING(erase) |
892f7f096ffb
erase.m: New string function for deleting substrings with a string.
Sahil Yadav
parents:
23220
diff
changeset
|
475 |
3361 | 476 @DOCSTRING(substr) |
3294 | 477 |
5582 | 478 @DOCSTRING(regexp) |
479 | |
480 @DOCSTRING(regexpi) | |
481 | |
6549 | 482 @DOCSTRING(regexprep) |
483 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7639
diff
changeset
|
484 @DOCSTRING(regexptranslate) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7639
diff
changeset
|
485 |
11103
d4fc294a19e4
include untabify docstring in the manual
John W. Eaton <jwe@octave.org>
parents:
10828
diff
changeset
|
486 @DOCSTRING(untabify) |
d4fc294a19e4
include untabify docstring in the manual
John W. Eaton <jwe@octave.org>
parents:
10828
diff
changeset
|
487 |
25412
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
488 @DOCSTRING(unicode_idx) |
922a93fc73ec
Add function to index characters in UTF-8 encoded strings.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
489 |
4167 | 490 @node String Conversions |
3294 | 491 @section String Conversions |
492 | |
6623 | 493 Octave supports various kinds of conversions between strings and |
8351
4d78baf20ded
improve string documentation
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
494 numbers. As an example, it is possible to convert a string containing |
6623 | 495 a hexadecimal number to a floating point number. |
496 | |
497 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
498 @group |
6623 | 499 hex2dec ("FF") |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
500 @result{} 255 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
501 @end group |
6623 | 502 @end example |
503 | |
3361 | 504 @DOCSTRING(bin2dec) |
3294 | 505 |
3361 | 506 @DOCSTRING(dec2bin) |
3294 | 507 |
3361 | 508 @DOCSTRING(dec2hex) |
3294 | 509 |
3361 | 510 @DOCSTRING(hex2dec) |
3294 | 511 |
3920 | 512 @DOCSTRING(dec2base) |
513 | |
514 @DOCSTRING(base2dec) | |
515 | |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
7081
diff
changeset
|
516 @DOCSTRING(num2hex) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
7081
diff
changeset
|
517 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
7081
diff
changeset
|
518 @DOCSTRING(hex2num) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
7081
diff
changeset
|
519 |
6623 | 520 @DOCSTRING(str2double) |
3920 | 521 |
6623 | 522 @DOCSTRING(strjust) |
6502 | 523 |
3361 | 524 @DOCSTRING(str2num) |
3294 | 525 |
3361 | 526 @DOCSTRING(tolower) |
3294 | 527 |
3361 | 528 @DOCSTRING(toupper) |
3294 | 529 |
23122
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
22755
diff
changeset
|
530 @DOCSTRING(unicode2native) |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
22755
diff
changeset
|
531 |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
22755
diff
changeset
|
532 @DOCSTRING(native2unicode) |
e310b5b6da6f
Add functions native2unicode and unicode2native (bug #49842).
Markus Mützel <markus.muetzel@gmx.de>
parents:
22755
diff
changeset
|
533 |
3428 | 534 @DOCSTRING(do_string_escapes) |
535 | |
3361 | 536 @DOCSTRING(undo_string_escapes) |
3294 | 537 |
4167 | 538 @node Character Class Functions |
3294 | 539 @section Character Class Functions |
540 | |
541 Octave also provides the following character class test functions | |
542 patterned after the functions in the standard C library. They all | |
543 operate on string arrays and return matrices of zeros and ones. | |
544 Elements that are nonzero indicate that the condition was true for the | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
9209
diff
changeset
|
545 corresponding character in the string array. For example: |
3294 | 546 |
547 @example | |
548 @group | |
549 isalpha ("!Q@@WERT^Y&") | |
550 @result{} [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ] | |
551 @end group | |
552 @end example | |
553 | |
3361 | 554 @DOCSTRING(isalnum) |
3294 | 555 |
3361 | 556 @DOCSTRING(isalpha) |
557 | |
6549 | 558 @DOCSTRING(isletter) |
559 | |
3361 | 560 @DOCSTRING(islower) |
3294 | 561 |
12581
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
562 @DOCSTRING(isupper) |
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
563 |
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
564 @DOCSTRING(isdigit) |
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
565 |
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
566 @DOCSTRING(isxdigit) |
3294 | 567 |
3361 | 568 @DOCSTRING(ispunct) |
3294 | 569 |
3361 | 570 @DOCSTRING(isspace) |
3294 | 571 |
12581
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
572 @DOCSTRING(iscntrl) |
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
573 |
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
574 @DOCSTRING(isgraph) |
3294 | 575 |
12581
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
576 @DOCSTRING(isprint) |
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
577 |
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
578 @DOCSTRING(isascii) |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7639
diff
changeset
|
579 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7639
diff
changeset
|
580 @DOCSTRING(isstrprop) |