<html lang="en"> <head> <title>Character Strings in Oct-Files - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Oct_002dFiles.html#Oct_002dFiles" title="Oct-Files"> <link rel="prev" href="Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles" title="Matrices and Arrays in Oct-Files"> <link rel="next" href="Cell-Arrays-in-Oct_002dFiles.html#Cell-Arrays-in-Oct_002dFiles" title="Cell Arrays in Oct-Files"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Character-Strings-in-Oct-Files"></a> <a name="Character-Strings-in-Oct_002dFiles"></a> <p> Next: <a rel="next" accesskey="n" href="Cell-Arrays-in-Oct_002dFiles.html#Cell-Arrays-in-Oct_002dFiles">Cell Arrays in Oct-Files</a>, Previous: <a rel="previous" accesskey="p" href="Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles">Matrices and Arrays in Oct-Files</a>, Up: <a rel="up" accesskey="u" href="Oct_002dFiles.html#Oct_002dFiles">Oct-Files</a> <hr> </div> <h4 class="subsection">A.1.3 Character Strings in Oct-Files</h4> <p>In Octave a character string is just a special <code>Array</code> class. Consider the example <pre class="example"><pre class="verbatim"> #include <octave/oct.h> DEFUN_DLD (stringdemo, args, , "String Demo") { int nargin = args.length(); octave_value_list retval; if (nargin != 1) print_usage (); else { charMatrix ch = args(0).char_matrix_value (); if (! error_state) { if (args(0).is_sq_string ()) retval(1) = octave_value (ch, true); else retval(1) = octave_value (ch, true, '\''); 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, true); } } return retval; } </pre></pre> <p>An example of the use of this function is <pre class="example"> 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 </pre> <p>One additional complication of strings in Octave is the difference between single quoted and double quoted strings. To find out if an <code>octave_value</code> contains a single or double quoted string an example is <pre class="example"> if (args(0).is_sq_string ()) octave_stdout << "First argument is a singularly quoted string\n"; else if (args(0).is_dq_string ()) octave_stdout << "First argument is a doubly quoted string\n"; </pre> <p>Note however, that both types of strings are represented by the <code>charNDArray</code> type, and so when assigning to an <code>octave_value</code>, the type of string should be specified. For example <pre class="example"> octave_value_list retval; charNDArray c; ... // Create single quoted string retval(1) = octave_value (ch, true, '\''); // Create a double quoted string retval(0) = octave_value (ch, true); </pre> </body></html>