#### 5.3.2 Concatenating Strings ¶

Strings can be concatenated using matrix notation (see Strings, Character Arrays) which is often the most natural method. For example:

    fullname = [fname ".txt"];
    email = ["<" user "@" domain ">"];

In each case it is easy to see what the final string will look like. This method is also the most efficient. When using matrix concatenation the parser immediately begins joining the strings without having to process the overhead of a function call and the input validation of the associated function.

The `newline` function can be used to join strings such that they appear as multiple lines of text when displayed.

`c =` newline ¶

  Return the character corresponding to a newline.

  This is equivalent to `"\n"`.

  Example Code

      joined_string = [newline "line1" newline "line2"]
      ⇒
      line1
      line2

  See also: strcat, strjoin, strsplit.

In addition, there are several other functions for concatenating string objects which can be useful in specific circumstances: `char`, `strvcat`, `strcat`, and `cstrcat`. Finally, the general purpose concatenation functions can be used: see cat, horzcat, and vertcat.

- All string concatenation functions except `cstrcat` convert numerical input into character data by taking the corresponding UTF-8 character for each element (or multi-byte sequence), as in the following example:

      char ([98, 97, 110, 97, 110, 97])
         ⇒  banana

  For conversion between locale encodings and UTF-8, see unicode2native and native2unicode.

- `char` and `strvcat` concatenate vertically, while `strcat` and `cstrcat` concatenate horizontally. For example:

      char ("an apple", "two pears")
          ⇒  an apple
             two pears

      strcat ("oc", "tave", " is", " good", " for you")
           ⇒  octave is good for you

- `char` generates an empty row in the output for each empty string in the input. `strvcat`, on the other hand, eliminates empty strings.

      char ("orange", "green", "", "red")
          ⇒  orange
             green

             red

      strvcat ("orange", "green", "", "red")
          ⇒  orange
             green
             red

- All string concatenation functions except `cstrcat` also accept cell array data (see Cell Arrays). `char` and `strvcat` convert cell arrays into character arrays, while `strcat` concatenates within the cells of the cell arrays:

      char ({"red", "green", "", "blue"})
           ⇒  red
              green

              blue

      strcat ({"abc"; "ghi"}, {"def"; "jkl"})
           ⇒
              {
                [1,1] = abcdef
                [2,1] = ghijkl
              }

- `strcat` removes trailing white space in the arguments (except within cell arrays), while `cstrcat` leaves white space untouched. Both kinds of behavior can be useful as can be seen in the examples:

      strcat (["dir1";"directory2"], ["/";"/"], ["file1";"file2"])
           ⇒  dir1/file1
              directory2/file2

      cstrcat (["thirteen apples"; "a banana"], [" 5$";" 1$"])
            ⇒  thirteen apples 5$
               a banana        1$

  Note that in the above example for `cstrcat`, the white space originates from the internal representation of the strings in a string array (see Character Arrays).

`C =` char `(A)` ¶
`C =` char `(A, …)` ¶
`C =` char `(str1, str2, …)` ¶
`C =` char `(cell_array)` ¶
`'' =` char `()` ¶

  Create a string array from one or more numeric matrices, character matrices, or cell arrays.

  Arguments are concatenated vertically. The returned values are padded with blanks as needed to make each row of the string array have the same length. Empty input strings are significant and will concatenated in the output.

  For numerical input, each element is converted to the corresponding ASCII character. A range error results if an input is outside the ASCII range (0-255).

  For cell arrays, each element is concatenated separately. Cell arrays converted through `char` can mostly be converted back with `cellstr`. For example:

      char ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"])
         ⇒  ["abc "
             "    "
             "98  "
             "99  "
             "d   "
             "str1"
             "half"]

  See also: strvcat, cellstr.

`C =` strvcat `(A)` ¶
`C =` strvcat `(A, …)` ¶
`C =` strvcat `(str1, str2, …)` ¶
`C =` strvcat `(cell_array)` ¶

  Create a character array from one or more numeric matrices, character matrices, or cell arrays.

  Arguments are concatenated vertically. The returned values are padded with blanks as needed to make each row of the string array have the same length. Unlike `char`, empty strings are removed and will not appear in the output.

  For numerical input, each element is converted to the corresponding ASCII character. A range error results if an input is outside the ASCII range (0-255).

  For cell arrays, each element is concatenated separately. Cell arrays converted through `strvcat` can mostly be converted back with `cellstr`. For example:

      strvcat ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"])
            ⇒  ["abc "
                "98  "
                "99  "
                "d   "
                "str1"
                "half"]

  See also: char, strcat, cstrcat.

`str =` strcat `(s1, s2, …)` ¶

  Return a string containing all the arguments concatenated horizontally.

  If the arguments are cell strings, `strcat` returns a cell string with the individual cells concatenated. For numerical input, each element is converted to the corresponding ASCII character. Trailing white space for any character string input is eliminated before the strings are concatenated. Note that cell string values do not have whitespace trimmed.

  For example:

      strcat ("|", " leading space is preserved", "|")
          ⇒  | leading space is preserved|

      strcat ("|", "trailing space is eliminated ", "|")
          ⇒  |trailing space is eliminated|

      strcat ("homogeneous space |", "  ", "| is also eliminated")
          ⇒  homogeneous space || is also eliminated

      s = [ "ab"; "cde" ];
      strcat (s, s, s)
          ⇒
              "ababab   "
              "cdecdecde"

      s = { "ab"; "cd " };
      strcat (s, s, s)
          ⇒
              {
                [1,1] = ababab
                [2,1] = cd cd cd
              }

  See also: cstrcat, char, strvcat.

`str =` cstrcat `(s1, s2, …)` ¶

  Return a string containing all the arguments concatenated horizontally with trailing white space preserved.

  For example:

      cstrcat ("ab   ", "cd")
            ⇒  "ab   cd"

      s = [ "ab"; "cde" ];
      cstrcat (s, s, s)
            ⇒  "ab ab ab "
               "cdecdecde"

  See also: strcat, char, strvcat.