A.1.3 Строки символов в Oct-файлах
Строка символов в Octave — это просто специальный Array класс. Рассмотрим пример:
#include <octave/oct.h>
DEFUN_DLD (stringdemo, args, , "String Demo")
{
if (args.length () != 1)
print_usage ();
octave_value_list retval;
charMatrix ch = args(0).char_matrix_value ();
retval(1) = octave_value (ch, '\''); // Single Quote String
octave_idx_type nr = ch.rows ();
for (octave_idx_type i = 0; i < nr / 2; i++)
{
std::string tmp = ch.row_as_string (i);
ch.insert (ch.row_as_string (nr-i-1).c_str (), i, 0);
ch.insert (tmp.c_str (), nr-i-1, 0);
}
retval(0) = octave_value (ch, '"'); // Double Quote String
return retval;
} Пример использования этой функции:
s0 = ["First String"; "Second String"];
[s1,s2] = stringdemo (s0)
⇒ s1 = Second String
First String
⇒ s2 = First String
Second String
typeinfo (s2)
⇒ sq_string
typeinfo (s1)
⇒ string Ещё одно осложнение при работе со строками в Octave — различие между строками в одинарных и двойных кавычках. Чтобы выяснить, содержит ли octave_value строку в одинарных или двойных кавычках, используйте одну из приведённых ниже проверок-предикатов.
if (args(0).is_sq_string ()) octave_stdout << "First argument is a single quoted string\n"; else if (args(0).is_dq_string ()) octave_stdout << "First argument is a double quoted string\n";
Однако обратите внимание: оба типа строк представлены типом charNDArray, поэтому при присваивании значения octave_value следует указывать тип строки. Например:
octave_value_list retval;
charNDArray ch;
...
// Create single quoted string
retval(1) = octave_value (ch); // default constructor is sq_string
OR
retval(1) = octave_value (ch, '\''); // explicitly create sq_string
// Create a double quoted string
retval(0) = octave_value (ch, '"');
© 1996–2026 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v9.4.0/Character-Strings-in-Oct_002dFiles.html