45 @deftypefn {Built-in Function} {} char (@var{x})\n\
46 @deftypefnx {Built-in Function} {} char (@var{x}, @dots{})\n\
47 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\
48 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\
49 Create a string array from one or more numeric matrices, character\n\
50 matrices, or cell arrays. Arguments are concatenated vertically.\n\
51 The returned values are padded with blanks as needed to make each row\n\
52 of the string array have the same length. Empty input strings are\n\
53 significant and will concatenated in the output.\n\
55 For numerical input, each element is converted\n\
56 to the corresponding ASCII character. A range error results if an input\n\
57 is outside the ASCII range (0-255).\n\
59 For cell arrays, each element is concatenated separately. Cell arrays\n\
61 @code{char} can mostly be converted back with @code{cellstr}.\n\
66 char ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, \"str1\", [\"ha\", \"lf\"])\n\
67 @result{} [\"abc \"\n\
76 @seealso{strvcat, cellstr}\n\
81 int nargin = args.
length ();
94 std::queue<string_vector> args_as_strings;
96 for (
int i = 0; i < nargin; i++)
102 error (
"char: unable to convert some args to strings");
113 if (s_max_len > max_len)
116 args_as_strings.push (s);
123 for (
int i = 0; i < nargin; i++)
126 args_as_strings.pop ();
132 for (
int j = 0; j < n; j++)
134 std::string t = s[j];
138 t += std::string (max_len - t_len,
' ');
144 result[k++] = std::string (max_len,
' ');
174 DEFUN (strvcat, args, ,
176 @deftypefn {Built-in Function} {} strvcat (@var{x})\n\
177 @deftypefnx {Built-in Function} {} strvcat (@var{x}, @dots{})\n\
178 @deftypefnx {Built-in Function} {} strvcat (@var{s1}, @var{s2}, @dots{})\n\
179 @deftypefnx {Built-in Function} {} strvcat (@var{cell_array})\n\
180 Create a character array from one or more numeric matrices, character\n\
181 matrices, or cell arrays. Arguments are concatenated vertically.\n\
182 The returned values are padded with blanks as needed to make each row\n\
183 of the string array have the same length. Unlike @code{char}, empty\n\
184 strings are removed and will not appear in the output.\n\
186 For numerical input, each element is converted\n\
187 to the corresponding ASCII character. A range error results if an input\n\
188 is outside the ASCII range (0-255).\n\
190 For cell arrays, each element is concatenated separately. Cell arrays\n\
192 @code{strvcat} can mostly be converted back with @code{cellstr}.\n\
197 strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, \"str1\", [\"ha\", \"lf\"])\n\
198 @result{} [\"abc \"\n\
206 @seealso{char, strcat, cstrcat}\n\
211 int nargin = args.
length ();
219 std::queue<string_vector> args_as_strings;
221 for (
int i = 0; i < nargin; i++)
227 error (
"strvcat: unable to convert some args to strings");
236 for (
size_t j = 0; j < n; j++)
245 if (s_max_len > max_len)
248 args_as_strings.push (s);
255 for (
int i = 0; i < nargin; i++)
258 args_as_strings.pop ();
264 for (
size_t j = 0; j < n; j++)
266 std::string t = s[j];
269 size_t t_len = t.
length ();
272 t += std::string (max_len - t_len,
' ');
305 DEFUN (ischar, args, ,
307 @deftypefn {Built-in Function} {} ischar (@var{x})\n\
308 Return true if @var{x} is a character array.\n\
309 @seealso{isfloat, isinteger, islogical, isnumeric, iscellstr, isa}\n\
314 int nargin = args.
length ();
346 bool (*str_op) (
const std::string&,
const std::string&,
353 bool s1_cell = arg0.
is_cell ();
355 bool s2_cell = arg1.
is_cell ();
357 if (s1_string && s2_string)
359 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
378 if (r == 0 || r == 1)
384 std::string s = r == 0 ? std::string () : str[0];
390 output(i) = str_op (cellstr(i), s, n);
397 if (cell(i).is_string ())
415 const std::string str2 = cell(0).string_value ();
418 output(i) = str_op (str[i], str2, n);
436 output(i) = str_op (str[i], cellstr(i), n);
444 output(i) = str_op (str[i],
456 else if (s1_cell && s2_cell)
490 const std::string str2 = cell2(0).string_value ();
496 output(i) = str_op (cellstr(i), str2, n);
505 const std::string str1 = cell1(i).string_value ();
506 output(i) = str_op (str1, str2, n);
516 error (
"%s: nonconformant cell arrays", fcn_name);
525 output (i) = str_op (cellstr1(i), cellstr2(i), n);
534 const std::string str1 = cell1(i).string_value ();
535 const std::string str2 = cell2(i).string_value ();
536 output(i) = str_op (str1, str2, n);
566 DEFUN (strcmp, args, ,
568 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\
569 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\
572 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
573 of the same size is returned, containing the values described above for\n\
574 every member of the cell array. The other argument may also be a cell\n\
575 array of strings (of the same size or with only one element), char matrix\n\
576 or character string.\n\
578 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp\n\
579 function returns 1 if the character strings are equal, and 0 otherwise.\n\
580 This is just the opposite of the corresponding C library function.\n\
581 @seealso{strcmpi, strncmp, strncmpi}\n\
586 if (args.length () == 2)
651 return (n > 0 && n <= l1 && n <= l2
652 && std::equal (s1.
data (), s1.
data () + n, s2.
data ()));
662 return (n > 0 && n <= l1 && n <= l2
663 && std::equal (s1.data (), s1.data () + n, s2.data ()));
666 DEFUN (strncmp, args, ,
668 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\
669 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are\n\
670 the same, and 0 otherwise.\n\
674 strncmp (\"abce\", \"abcd\", 3)\n\
679 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
680 of the same size is returned, containing the values described above for\n\
681 every member of the cell array. The other argument may also be a cell\n\
682 array of strings (of the same size or with only one element), char matrix\n\
683 or character string.\n\
687 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\
688 @result{} [1, 0, 1]\n\
692 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmp\n\
693 function returns 1 if the character strings are equal, and 0 otherwise.\n\
694 This is just the opposite of the corresponding C library function.\n\
695 @seealso{strncmpi, strcmp, strcmpi}\n\
700 if (args.length () == 3)
712 error (
"strncmp: N must be greater than 0");
738 {
return std::toupper (x) == std::toupper (y); }
755 return (s1.size () == s2.size ()
756 && std::equal (s1.data (), s1.data () + s1.size (), s2.data (),
762 @deftypefn {Built-in Function} {} strcmpi (@var{s1}, @var{s2})\n\
763 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\
764 disregarding case of alphabetic characters, and 0 otherwise.\n\
766 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
767 of the same size is returned, containing the values described above for\n\
768 every member of the cell array. The other argument may also be a cell\n\
769 array of strings (of the same size or with only one element), char matrix\n\
770 or character string.\n\
772 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp\n\
773 function returns 1 if the character strings are equal, and 0 otherwise.\n\
774 This is just the opposite of the corresponding C library function.\n\
776 @strong{Caution:} National alphabets are not supported.\n\
777 @seealso{strcmp, strncmp, strncmpi}\n\
782 if (args.length () == 2)
803 return (n > 0 && n <= l1 && n <= l2
814 return (n > 0 && n <= l1 && n <= l2
815 && std::equal (s1.data (), s1.data () + n, s2.data (),
821 @deftypefn {Built-in Function} {} strncmpi (@var{s1}, @var{s2}, @var{n})\n\
822 Return 1 if the first @var{n} character of @var{s1} and @var{s2} are the\n\
823 same, disregarding case of alphabetic characters, and 0 otherwise.\n\
825 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
826 of the same size is returned, containing the values described above for\n\
827 every member of the cell array. The other argument may also be a cell\n\
828 array of strings (of the same size or with only one element), char matrix\n\
829 or character string.\n\
831 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmpi\n\
832 function returns 1 if the character strings are equal, and 0 otherwise.\n\
833 This is just the opposite of the corresponding C library function.\n\
835 @strong{Caution:} National alphabets are not supported.\n\
836 @seealso{strncmp, strcmp, strcmpi}\n\
841 if (args.length () == 3)
853 error (
"strncmpi: N must be greater than 0");
866 DEFUN (list_in_columns, args, ,
868 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width}, @var{prefix})\n\
869 Return a string containing the elements of @var{arg} listed in\n\
870 columns with an overall maximum width of @var{width} and optional\n\
871 prefix @var{prefix}. The argument @var{arg} must be a cell array\n\
872 of character strings or a character array. If @var{width} is not\n\
873 specified or is an empty matrix, or less than or equal to zero,\n\
874 the width of the terminal screen is used.\n\
875 Newline characters are used to break the lines in the output string.\n\
877 @c Set example in small font to prevent overfull line\n\
881 list_in_columns (@{\"abc\", \"def\", \"ghijkl\", \"mnop\", \"qrs\", \"tuv\"@}, 20)\n\
882 @result{} abc mnop\n\
888 Variables in the current scope:\n\
890 Attr Name Size Bytes Class\n\
891 ==== ==== ==== ===== =====\n\
894 Total is 37 elements using 37 bytes\n\
898 @seealso{terminal_size}\n\
903 int nargin = args.
length ();
905 if (nargin < 1 || nargin > 3)
915 error (
"list_in_columns: expecting cellstr or char array");
921 if (nargin > 1 && ! args(1).is_empty ())
923 width = args(1).int_value ();
927 error (
"list_in_columns: WIDTH must be an integer");
936 if (args(2).is_string ())
942 error (
"list_in_columns: PREFIX must be a character string");
948 error (
"list_in_columns: PREFIX must be a character string");
953 std::ostringstream buf;