# Text in String and Character Arrays

There are two ways to represent text in MATLAB®. You can store text in string arrays. And in any version of MATLAB, you can store text in character arrays. A typical use for character arrays is to store pieces of text as character vectors. MATLAB displays strings with double quotes and character vectors with single quotes.

### Represent Text with String Arrays

You can store any 1-by-`n` sequence of characters as a string, using the `string` data type. Enclose text in double quotes to create a string.

`str = "Hello, world"`
```str = "Hello, world" ```

Though the text `"Hello, world"` is 12 characters long, `str` itself is a 1-by-1 string, or string scalar. You can use a string scalar to specify a file name, plot label, or any other piece of textual information.

To find the number of characters in a string, use the `strlength` function.

`n = strlength(str)`
```n = 12 ```

If the text includes double quotes, use two double quotes within the definition.

`str = "They said, ""Welcome!"" and waved."`
```str = "They said, "Welcome!" and waved." ```

To add text to the end of a string, use the plus operator, `+`. If a variable can be converted to a string, then `plus` converts it and appends it.

```fahrenheit = 71; celsius = (fahrenheit-32)/1.8; tempText = "temperature is " + celsius + "C"```
```tempText = "temperature is 21.6667C" ```

You can also concatenate text using the `append` function.

`tempText2 = append("Today's ",tempText)`
```tempText2 = "Today's temperature is 21.6667C" ```

The `string` function can convert different types of inputs, such as numeric, datetime, duration, and categorical values. For example, convert the output of `pi` to a string.

`ps = string(pi)`
```ps = "3.1416" ```

You can store multiple pieces of text in a string array. Each element of the array can contain a string having a different number of characters, without padding.

```str = ["Mercury","Gemini","Apollo";... "Skylab","Skylab B","ISS"]```
```str = 2x3 string "Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS" ```

`str` is a 2-by-3 string array. You can find the lengths of the strings with the `strlength` function.

`N = strlength(str)`
```N = 2×3 7 6 6 6 8 3 ```

String arrays are supported throughout MATLAB and MathWorks® products. Functions that accept character arrays (and cell arrays of character vectors) as inputs also accept string arrays.

### Represent Text with Character Vectors

To store a 1-by-`n` sequence of characters as a character vector, using the `char` data type, enclose it in single quotes.

`chr = 'Hello, world'`
```chr = 'Hello, world' ```

The text `'Hello, world'` is 12 characters long, and `chr` stores it as a 1-by-12 character vector.

`whos chr`
``` Name Size Bytes Class Attributes chr 1x12 24 char ```

If the text includes single quotes, use two single quotes within the definition.

`chr = 'They said, ''Welcome!'' and waved.'`
```chr = 'They said, 'Welcome!' and waved.' ```

Character vectors have two principal uses:

• To specify single pieces of text, such as file names and plot labels.

• To represent data that is encoded using characters. In such cases, you might need easy access to individual characters.

For example, you can store a DNA sequence as a character vector.

`seq = 'GCTAGAATCC';`

You can access individual characters or subsets of characters by indexing, just as you would index into a numeric array.

`seq(4:6)`
```ans = 'AGA' ```

Concatenate character vector with square brackets, just as you concatenate other types of arrays.

`seq2 = [seq 'ATTAGAAACC']`
```seq2 = 'GCTAGAATCCATTAGAAACC' ```

Starting in R2019a, you also can concatenate text using `append`. The `append` function is recommended because it treats string arrays, character vectors, and cell arrays of character vectors consistently.

`seq2 = append(seq,'ATTAGAAACC')`
```seq2 = 'GCTAGAATCCATTAGAAACC' ```

MATLAB functions that accept string arrays as inputs also accept character vectors and cell arrays of character vectors.