Main Content

Compare Text

Compare text in character arrays and string arrays in different ways. You can compare string arrays and character vectors with relational operators and 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.

Create two string scalars. You can create strings using double quotes.

str1 = "Hello";
str2 = "World";
str1,str2
str1 = 
"Hello"
str2 = 
"World"

Compare str1 and str2 for equality.

str1 == str2
ans = logical
   0

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

str1 = ["Mercury","Gemini","Apollo";...
        "Skylab","Skylab B","International Space Station"];
str2 = "Apollo";
str1 == str2
ans = 2x3 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 = 2x3 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 = 
"Gemini"

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

TF = (str1 ~= chr)
TF = 2x3 logical array

   1   0   1
   1   1   1

str1(TF)
ans = 5x1 string
    "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 = ["Mercury","Mars","Apollo";...
        "Jupiter","Saturn","Neptune"];
TF = (str1 == str2)
TF = 2x3 logical array

   1   0   1
   0   0   0

Index into str1 to extract the matches.

str1(TF)
ans = 2x1 string
    "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.

"ABC" < "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 = ["Sanchez","Jones","de Ponte","Crosby","Nash"]; 
TF = (str > "Matthews")
TF = 1x5 logical array

   1   0   1   0   1

str(TF)
ans = 1x3 string
    "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 = 1x5 string
    "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 = 2x3 string
    "Jupiter"    "Mars"      "Apollo" 
    "Mercury"    "Saturn"    "Neptune"

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

sort(str2,2)
ans = 2x3 string
    "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 = 3x1 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 = 1x30 logical array

   0   0   0   0   1   0   0   0   0   0   1   0   0   0   1   0   0   0   0   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 = "A horse! A horse! My kingdom for a horse!"
str = 
"A horse! A horse! My kingdom for a horse!"
isstrprop(str,"punct")
ans = 1x41 logical array

   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1

Determine which characters in the character vector chr are letters.

isstrprop(chr,"alpha")
ans = 1x30 logical array

   1   1   1   1   0   1   1   1   1   1   0   1   1   1   0   1   1   1   1   1   0   1   1   1   1   1   0   1   1   1

See Also

| | | | | | | | | |

Related Topics