Next: , Previous: , Up: Strings   [Contents][Index]

### 5.2 Character Arrays

The string representation used by Octave is an array of characters, so internally the string `"dddddddddd"` is actually a row vector of length 10 containing the value 100 in all places (100 is the ASCII code of `"d"`). This lends itself to the obvious generalization to character matrices. Using a matrix of characters, it is possible to represent a collection of same-length strings in one variable. The convention used in Octave is that each row in a character matrix is a separate string, but letting each column represent a string is equally possible.

The easiest way to create a character matrix is to put several strings together into a matrix.

```collection = [ "String #1"; "String #2" ];
```

This creates a 2-by-9 character matrix.

The function `ischar` can be used to test if an object is a character matrix.

Built-in Function: ischar (x)

Return true if x is a character array.

See also: isfloat, isinteger, islogical, isnumeric, iscellstr, isa.

To test if an object is a string (i.e., a character vector and not a character matrix) you can use the `ischar` function in combination with the `isvector` function as in the following example:

```ischar (collection)
⇒ 1

ischar (collection) && isvector (collection)
⇒ 0

ischar ("my string") && isvector ("my string")
⇒ 1
```

One relevant question is, what happens when a character matrix is created from strings of different length. The answer is that Octave puts blank characters at the end of strings shorter than the longest string. It is possible to use a different character than the blank character using the `string_fill_char` function.

Built-in Function: val = string_fill_char ()
Built-in Function: old_val = string_fill_char (new_val)
Built-in Function: string_fill_char (new_val, "local")

Query or set the internal variable used to pad all rows of a character matrix to the same length.

The value must be a single character and the default is `" "` (a single space). For example:

```string_fill_char ("X");
[ "these"; "are"; "strings" ]
⇒  "theseXX"
"areXXXX"
"strings"
```

When called from inside a function with the `"local"` option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

This shows a problem with character matrices. It simply isn’t possible to represent strings of different lengths. The solution is to use a cell array of strings, which is described in Cell Arrays of Strings.

Next: , Previous: , Up: Strings   [Contents][Index]