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.\n\
52 Arguments are concatenated vertically. The returned values are padded with\n\
53 blanks as needed to make each row of the string array have the same length.\n\
54 Empty input strings are significant and will concatenated in the output.\n\
56 For numerical input, each element is converted to the corresponding ASCII\n\
57 character. A range error results if an input is outside the ASCII range\n\
60 For cell arrays, each element is concatenated separately. Cell arrays\n\
61 converted through @code{char} can mostly be converted back with\n\
62 @code{cellstr}. For example:\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.\n\
183 Arguments are concatenated vertically. The returned values are padded with\n\
184 blanks as needed to make each row of the string array have the same length.\n\
185 Unlike @code{char}, empty strings are removed and will not appear in the\n\
188 For numerical input, each element is converted to the corresponding ASCII\n\
189 character. A range error results if an input is outside the ASCII range\n\
192 For cell arrays, each element is concatenated separately. Cell arrays\n\
193 converted through @code{strvcat} can mostly be converted back with\n\
194 @code{cellstr}. For example:\n\
198 strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, \"str1\", [\"ha\", \"lf\"])\n\
199 @result{} [\"abc \"\n\
207 @seealso{char, strcat, cstrcat}\n\
212 int nargin = args.
length ();
220 std::queue<string_vector> args_as_strings;
222 for (
int i = 0; i < nargin; i++)
228 error (
"strvcat: unable to convert some args to strings");
237 for (
size_t j = 0; j < n; j++)
246 if (s_max_len > max_len)
249 args_as_strings.push (s);
256 for (
int i = 0; i < nargin; i++)
259 args_as_strings.pop ();
265 for (
size_t j = 0; j < n; j++)
267 std::string t = s[j];
270 size_t t_len = t.
length ();
273 t += std::string (max_len - t_len,
' ');
306 DEFUN (ischar, args, ,
308 @deftypefn {Built-in Function} {} ischar (@var{x})\n\
309 Return true if @var{x} is a character array.\n\
310 @seealso{isfloat, isinteger, islogical, isnumeric, iscellstr, isa}\n\
315 int nargin = args.
length ();
347 bool (*str_op) (
const std::string&,
const std::string&,
354 bool s1_cell = arg0.
is_cell ();
356 bool s2_cell = arg1.
is_cell ();
358 if (s1_string && s2_string)
360 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
379 if (r == 0 || r == 1)
385 std::string s = r == 0 ? std::string () : str[0];
391 output(i) = str_op (cellstr(i), s, n);
398 if (cell(i).is_string ())
416 const std::string str2 = cell(0).string_value ();
419 output(i) = str_op (str[i], str2, n);
437 output(i) = str_op (str[i], cellstr(i), n);
445 output(i) = str_op (str[i],
457 else if (s1_cell && s2_cell)
491 const std::string str2 = cell2(0).string_value ();
497 output(i) = str_op (cellstr(i), str2, n);
506 const std::string str1 = cell1(i).string_value ();
507 output(i) = str_op (str1, str2, n);
517 error (
"%s: nonconformant cell arrays", fcn_name);
526 output (i) = str_op (cellstr1(i), cellstr2(i), n);
535 const std::string str1 = cell1(i).string_value ();
536 const std::string str2 = cell2(i).string_value ();
537 output(i) = str_op (str1, str2, n);
567 DEFUN (strcmp, args, ,
569 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\
570 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\
573 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
574 of the same size is returned, containing the values described above for\n\
575 every member of the cell array. The other argument may also be a cell\n\
576 array of strings (of the same size or with only one element), char matrix\n\
577 or character string.\n\
579 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp\n\
580 function returns 1 if the character strings are equal, and 0 otherwise.\n\
581 This is just the opposite of the corresponding C library function.\n\
582 @seealso{strcmpi, strncmp, strncmpi}\n\
587 if (args.length () == 2)
653 return (n > 0 && n <= l1 && n <= l2
654 && std::equal (s1.
data (), s1.
data () + n, s2.
data ()));
665 return (n > 0 && n <= l1 && n <= l2
666 && std::equal (s1.data (), s1.data () + n, s2.data ()));
669 DEFUN (strncmp, args, ,
671 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\
672 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are\n\
673 the same, and 0 otherwise.\n\
677 strncmp (\"abce\", \"abcd\", 3)\n\
682 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
683 of the same size is returned, containing the values described above for\n\
684 every member of the cell array. The other argument may also be a cell\n\
685 array of strings (of the same size or with only one element), char matrix\n\
686 or character string.\n\
690 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\
691 @result{} [1, 0, 1]\n\
695 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmp\n\
696 function returns 1 if the character strings are equal, and 0 otherwise.\n\
697 This is just the opposite of the corresponding C library function.\n\
698 @seealso{strncmpi, strcmp, strcmpi}\n\
703 if (args.length () == 3)
715 error (
"strncmp: N must be greater than 0");
741 {
return std::toupper (x) == std::toupper (y); }
758 return (s1.size () == s2.size ()
759 && std::equal (s1.data (), s1.data () + s1.size (), s2.data (),
765 @deftypefn {Built-in Function} {} strcmpi (@var{s1}, @var{s2})\n\
766 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\
767 disregarding case of alphabetic characters, and 0 otherwise.\n\
769 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
770 of the same size is returned, containing the values described above for\n\
771 every member of the cell array. The other argument may also be a cell\n\
772 array of strings (of the same size or with only one element), char matrix\n\
773 or character string.\n\
775 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp\n\
776 function returns 1 if the character strings are equal, and 0 otherwise.\n\
777 This is just the opposite of the corresponding C library function.\n\
779 @strong{Caution:} National alphabets are not supported.\n\
780 @seealso{strcmp, strncmp, strncmpi}\n\
785 if (args.length () == 2)
807 return (n > 0 && n <= l1 && n <= l2
819 return (n > 0 && n <= l1 && n <= l2
820 && std::equal (s1.data (), s1.data () + n, s2.data (),
826 @deftypefn {Built-in Function} {} strncmpi (@var{s1}, @var{s2}, @var{n})\n\
827 Return 1 if the first @var{n} character of @var{s1} and @var{s2} are the\n\
828 same, disregarding case of alphabetic characters, and 0 otherwise.\n\
830 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
831 of the same size is returned, containing the values described above for\n\
832 every member of the cell array. The other argument may also be a cell\n\
833 array of strings (of the same size or with only one element), char matrix\n\
834 or character string.\n\
836 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmpi\n\
837 function returns 1 if the character strings are equal, and 0 otherwise.\n\
838 This is just the opposite of the corresponding C library function.\n\
840 @strong{Caution:} National alphabets are not supported.\n\
841 @seealso{strncmp, strcmp, strcmpi}\n\
846 if (args.length () == 3)
858 error (
"strncmpi: N must be greater than 0");
871 DEFUN (list_in_columns, args, ,
873 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width}, @var{prefix})\n\
874 Return a string containing the elements of @var{arg} listed in columns with\n\
875 an overall maximum width of @var{width} and optional prefix @var{prefix}.\n\
877 The argument @var{arg} must be a cell array of character strings or a\n\
880 If @var{width} is not specified or is an empty matrix, or less than or equal\n\
881 to zero, the width of the terminal screen is used. Newline characters are\n\
882 used to break the lines in the output string. For example:\n\
883 @c Set example in small font to prevent overfull line\n\
887 list_in_columns (@{\"abc\", \"def\", \"ghijkl\", \"mnop\", \"qrs\", \"tuv\"@}, 20)\n\
888 @result{} abc mnop\n\
894 Variables in the current scope:\n\
896 Attr Name Size Bytes Class\n\
897 ==== ==== ==== ===== =====\n\
900 Total is 37 elements using 37 bytes\n\
904 @seealso{terminal_size}\n\
909 int nargin = args.
length ();
911 if (nargin < 1 || nargin > 3)
921 error (
"list_in_columns: ARG must be a cellstr or char array");
927 if (nargin > 1 && ! args(1).is_empty ())
929 width = args(1).int_value ();
933 error (
"list_in_columns: WIDTH must be an integer");
942 if (args(2).is_string ())
946 error (
"list_in_columns: PREFIX must be a string");
951 std::ostringstream buf;
charNDArray char_array_value(bool frc_str_conv=false) const
octave_idx_type max_length(void) const
static bool strncmpi_array_op(const charNDArray &s1, const charNDArray &s2, octave_idx_type n)
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
static bool strcmpi_str_op(const std::string &s1, const std::string &s2, octave_idx_type)
bool is_defined(void) const
std::ostream & list_in_columns(std::ostream &, int width=0, const std::string &prefix=std::string()) const
static bool strncmp_str_op(const std::string &s1, const std::string &s2, octave_idx_type n)
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
static bool strcmpi_array_op(const charNDArray &s1, const charNDArray &s2, octave_idx_type)
static octave_value do_strcmp_fun(const octave_value &arg0, const octave_value &arg1, octave_idx_type n, const char *fcn_name, bool(*array_op)(const charNDArray &, const charNDArray &, octave_idx_type), bool(*str_op)(const std::string &, const std::string &, octave_idx_type))
string_vector all_strings(bool pad=false) const
Cell cell_value(void) const
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type r2
Array< std::string > cellstr_value(void) const
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
static bool strcmp_str_op(const std::string &s1, const std::string &s2, octave_idx_type)
octave_value convert_to_str(bool pad=false, bool force=false, char type= '\'') const
octave_idx_type numel(const octave_value_list &idx)
std::string string_value(bool force=false) const
static bool strncmp_array_op(const charNDArray &s1, const charNDArray &s2, octave_idx_type n)
static bool strcmp_array_op(const charNDArray &s1, const charNDArray &s2, octave_idx_type)
bool is_string(void) const
OCTAVE_EXPORT octave_value_list Fstrcmpi(const octave_value_list &args, int)
const T * data(void) const
#define DEFUNX(name, fname, args_name, nargout_name, doc)
bool is_cellstr(void) const
OCTAVE_EXPORT octave_value_list Fstrncmpi(const octave_value_list &args, int)
bool is_cellstr(void) const
octave_idx_type length(void) const
dim_vector dims(void) const
static bool strncmpi_str_op(const std::string &s1, const std::string &s2, octave_idx_type n)
octave_idx_type length(void) const
Number of elements in the array.
bool is_dq_string(void) const
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type r1
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
F77_RET_T const double * x
bool operator()(char x, char y) const