Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

count

Count occurrences of pattern in string

Syntax

A = count(str,pattern)
A = count(str,pattern,'IgnoreCase',true)

Description

example

A = count(str,pattern) returns the number of occurrences of pattern in each element of str. The size of A is the same as the size of str.

If pattern is an array containing multiple patterns, then count returns the total number of occurrences of all elements of pattern in str.

example

A = count(str,pattern,'IgnoreCase',true) ignores case when counting the number of occurrences of pattern.

Examples

collapse all

Count the number of occurrences of the word red in the string array.

Starting in R2017a, you can create strings using double quotes instead of the string function.

str = ["red green red red blue blue green";
       "green red blue green green blue"]
str = 2x1 string array
    "red green red red blue blue green"
    "green red blue green green blue"

A = count(str,"red")
A = 

     3
     1

Count the sum of occurrences of red and blue in the string array.

Starting in R2017a, you can create strings using double quotes instead of the string function.

str = ["red green blue";
       "green red blue green blue"]
str = 2x1 string array
    "red green blue"
    "green red blue green blue"

count returns 2 for the first string because red and blue each occur once. count returns 3 for the second string because red occurs once and blue occurs twice.

A = count(str,["red","blue"])
A = 

     2
     3

Count the number of occurrences of the letter E in a string array that contains names, ignoring case.

Starting in R2017a, you can create strings using double quotes instead of the string function.

str = ["Edgar Allan Poe";"Louisa May Alcott"]
str = 2x1 string array
    "Edgar Allan Poe"
    "Louisa May Alcott"

A = count(str,'E','IgnoreCase',true)
A = 

     2
     0

Count the number of times al occurs in the word alphabetical.

chr = 'alphabetical'
chr = 
'alphabetical'
A = count(chr,'al')
A = 2

Input Arguments

collapse all

Input text, specified as a string array, a character vector, or a cell array of character vectors.

Data Types: string | char | cell

Search pattern, specified as a string array, a character vector, or a cell array of character vectors.

Data Types: string | char | cell

Extended Capabilities

Introduced in R2016b

Was this topic helpful?