Hauptinhalt

Compare Text

MATLAB® provides several ways to compare text in string arrays and character arrays. For instance, you can use relational operators, the matches function, or the strcmp function. You can sort string arrays using the sort function, just like with any other array type. You can also inspect characters in pieces of text, for example, to 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 using the relational operators == and ~=. These operators perform case-sensitive comparisons and return a logical array that has 1 where the relation is true and 0 where it is not.

For example, create two string scalars. You can create strings using double quotes.

str1 = "Hello"
str1 = 
"Hello"
str2 = "World"
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 = 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 using a relational operator.

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

   0   1   0
   0   0   0

You can use logical arrays to index into an array. For example, index into str1 with TF to extract the string elements that matched 'Gemini'.

str1(TF)
ans = 
"Gemini"

Compare for inequality using the ~= operator.

TF = (str1 ~= chr)
TF = 2×3 logical array

   1   0   1
   1   1   1

Index into str1 to extract the elements that do not match 'Gemini'.

str1(TF)
ans = 5×1 string
    "Mercury"
    "Skylab"
    "Skylab B"
    "Apollo"
    "International Space Station"

Compare two nonscalar string arrays. When you compare two nonscalar string arrays using relational operators, the arrays must be the same size.

str2 = ["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 elements that are equal.

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

Compare String Arrays for Matches

Alternatively, you can use the matches function to compare string arrays. For example:

matches("Hello","World")
ans = logical
   0

matches(["Mercury","Gemini","Apollo"],"Apollo")
ans = 1×3 logical array

   0   0   1

When comparing two nonscalar string arrays, matches compares each element of the first array to all elements of the second array. Therefore, matches does not require the two nonscalar arrays to have the same size.

For example, compare two nonscalar arrays of different sizes. The matches function finds elements of str1 that match either "Apollo" or "Mercury".

str1 = ["Mercury","Gemini","Apollo"; ...
    "Skylab","Skylab B","International Space Station"];
str2 = ["Apollo" "Mercury"];
matches(str1,str2)
ans = 2×3 logical array

   1   0   1
   0   0   0

You can also use matches to match a string array with a pattern. For example, find the string elements in str1 that contain only letters.

pat = lettersPattern;
matches(str1,pat)
ans = 2×3 logical array

   1   1   1
   1   0   0

The matches function also supports character vectors and cell arrays of character vectors.

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 = 1×5 logical array

   1   0   1   0   1

str(TF)
ans = 1×3 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 = 1×5 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(str1)
ans = 2×3 string
    "Mercury"    "Gemini"      "Apollo"                     
    "Skylab"     "Skylab B"    "International Space Station"

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

sort(str1,2)
ans = 2×3 string
    "Apollo"                         "Gemini"    "Mercury" 
    "International Space Station"    "Skylab"    "Skylab B"

Compare Character Vectors

You can compare character vectors and cell arrays of character vectors using the strcmp function. You can use the startsWith function to determine if the character vectors start with a specific pattern.

For example, compare two character vectors:

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

Note that the MATLAB strcmp function 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.

Determine if chr1 starts with the characters in chr2.

TF = startsWith(chr1,chr2)
TF = logical
   1

The startsWith function also supports string arrays.

Compare two cell arrays of character vectors. The strcmp function performs element-wise comparisons and 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

The strcmp function also supports string arrays.

Compare Text Ignoring Case

For case-insensitive comparisons, you can use strcmpi, or use matches with the IgnoreCase name-value argument.

For example, compare character vectors for equality, ignoring case.

chr1 = 'Hello';
chr2 = 'HELLO';
strcmpi(chr1,chr2)
ans = logical
   1

matches(chr1,chr2,IgnoreCase=true)
ans = logical
   1

Compare two string arrays, ignoring case. The strcmpi function performs element-wise comparisons.

str1 = ["Hello" "Ciao" "Bonjour"];
str2 = ["HELLO" "BONJOUR" "HOLA"];
strcmpi(str1,str2)
ans = 1×3 logical array

   1   0   0

The matches function with IgnoreCase set to true finds elements of str1 that match any element of str2, ignoring case.

matches(str1,str2,IgnoreCase=true)
ans = 1×3 logical array

   1   0   1

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

   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 = 1×41 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 = 1×30 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

| | | | | | | | | | | | | | | |

Topics