# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

## Compare Text

Compare text in character arrays and string arrays in different ways. Starting in R2016b, you can create string arrays with the `string` function, and compare them with relational operators. You also can compare string arrays and character vectors with the `strcmp` function. You can sort string arrays using the `sort` function, just as you would sort arrays of any other type. MATLAB® also provides functions to inspect characters in pieces of text. For example, you can determine which characters in a character vector or string array are letters or space characters.

### Compare String Arrays for Equality

You can compare string arrays for equality with the relational operators `==` and `~=`. When you compare string arrays, the output is a logical array that has `1` where the relation is true, and `0` where it is not true.

Compare two string scalars for equality.

```str1 = string('Hello'); str2 = string('World'); str1 == str2 ```
```ans = logical 0 ```

Compare a string array with multiple elements to a string scalar.

```C = {'Mercury','Gemini','Apollo';... 'Skylab','Skylab B','International Space Station'}; str1 = string(C); str2 = string('Apollo'); str1 == str2 ```
```ans = 2×3 logical array 0 0 1 0 0 0 ```

Compare a string array to a character vector. As long as one of the variables is a string array, you can make the comparison.

```chr = 'Gemini'; TF = (str1 == chr) ```
```TF = 2×3 logical array 0 1 0 0 0 0 ```

Index into `str1` with `TF` to extract the string elements that matched `Gemini`. You can use logical arrays to index into an array.

```str1(TF) ```
```ans = string "Gemini" ```

Compare for inequality using the `~=` operator. Index into `str1` to extract the elements that do not match `'Gemini'`.

```TF = (str1 ~= chr) ```
```TF = 2×3 logical array 1 0 1 1 1 1 ```
```str1(TF) ```
```ans = 5×1 string array "Mercury" "Skylab" "Skylab B" "Apollo" "International Space Station" ```

Compare two nonscalar string arrays. When you compare two nonscalar arrays, they must be the same size.

```str2 = string({'Mercury','Mars','Apollo';... 'Jupiter','Saturn','Neptune'}); TF = (str1 == str2) ```
```TF = 2×3 logical array 1 0 1 0 0 0 ```

Index into `str1` to extract the matches.

```str1(TF) ```
```ans = 2×1 string array "Mercury" "Apollo" ```

### Compare String Arrays with Other Relational Operators

You can also compare strings with the relational operators `>`, `>=`, `<`, and `<=`. Strings that start with uppercase letters come before strings that start with lowercase letters. For example, the string `"ABC"` is less than `"abc"`. Digits and some punctuation marks also come before letters.

```string('ABC') < string('abc') ```
```ans = logical 1 ```

Compare a string array that contains names to another name with the `>` operator. The names `Sanchez`, `de Ponte`, and `Nash` come after `Matthews`, because `S`, `d`, and `N` all are greater than `M`.

```str = string({'Sanchez','Jones','de Ponte','Crosby','Nash'}); TF = (str > 'Matthews') ```
```TF = 1×5 logical array 1 0 1 0 1 ```
```str(TF) ```
```ans = 1×3 string array "Sanchez" "de Ponte" "Nash" ```

### Sort String Arrays

You can sort string arrays. MATLAB® stores characters as Unicode® using the UTF-16 character encoding scheme. Character and string arrays are sorted according to the UTF-16 code point order. For the characters that are also the ASCII characters, this order means that uppercase letters come before lowercase letters. Digits and some punctuation also come before letters.

Sort the string array `str`.

```sort(str) ```
```ans = 1×5 string array "Crosby" "Jones" "Nash" "Sanchez" "de Ponte" ```

Sort a 2-by-3 string array. The `sort` function sorts the elements in each column separately.

```sort(str2) ```
```ans = 2×3 string array "Jupiter" "Mars" "Apollo" "Mercury" "Saturn" "Neptune" ```

To sort the elements in each row, sort `str2` along the second dimension.

```sort(str2,2) ```
```ans = 2×3 string array "Apollo" "Mars" "Mercury" "Jupiter" "Neptune" "Saturn" ```

### Compare Character Vectors

You can compare character vectors and cell arrays of character vectors to each other. Use the `strcmp` function to compare two character vectors, or `strncmp` to compare the first `N` characters. You also can use `strcmpi` and `strncmpi` for case-insensitive comparisons.

Compare two character vectors with the `strcmp` function. `chr1` and `chr2` are not equal.

```chr1 = 'hello'; chr2 = 'help'; TF = strcmp(chr1,chr2) ```
```TF = logical 0 ```

Note that the MATLAB `strcmp` differs from the C version of `strcmp`. The C version of `strcmp` returns `0` when two character arrays are the same, not when they are different.

Compare the first two characters with the `strncmp` function. `TF` is `1` because both character vectors start with the characters `he`.

```TF = strncmp(chr1,chr2,2) ```
```TF = logical 1 ```

Compare two cell arrays of character vectors. `strcmp` returns a logical array that is the same size as the cell arrays.

```C1 = {'pizza'; 'chips'; 'candy'}; C2 = {'pizza'; 'chocolate'; 'pretzels'}; strcmp(C1,C2) ```
```ans = 3×1 logical array 1 0 0 ```

### Inspect Characters in String and Character Arrays

You can inspect the characters in string arrays or character arrays with the `isstrprop`, `isletter`, and `isspace` functions.

• The `isstrprop` inspects characters in either string arrays or character arrays.

• The `isletter` and `isspace` functions inspect characters in character arrays only.

Determine which characters in a character vector are space characters. `isspace` returns a logical vector that is the same size as `chr`.

```chr = 'Four score and seven years ago'; TF = isspace(chr) ```
```TF = 1×30 logical array Columns 1 through 19 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 Columns 20 through 30 0 1 0 0 0 0 0 1 0 0 0 ```

The `isstrprop` function can query characters for many different traits. `isstrprop` can determine whether characters in a string or character vector are letters, alphanumeric characters, decimal or hexadecimal digits, or punctuation characters.

Determine which characters in a string are punctuation marks. `isstrprop` returns a logical vector whose length is equal to the number of characters in `str`.

```str = string('A horse! A horse! My kingdom for a horse!') ```
```str = string "A horse! A horse! My kingdom for a horse!" ```
```isstrprop(str,'punct') ```
```ans = 1×41 logical array Columns 1 through 19 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 Columns 20 through 38 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 39 through 41 0 0 1 ```

Determine which characters in the character vector `chr` are letters.

```isstrprop(chr,'alpha') ```
```ans = 1×30 logical array Columns 1 through 19 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 Columns 20 through 30 1 0 1 1 1 1 1 0 1 1 1 ```