How can I find the number of letters in a string? Just letters (A-Z, a-z)?

31 views (last 30 days)
How can I find the number of letters in a string? Just letters (A-Z, a-z)?

Accepted Answer

Star Strider
Star Strider on 23 May 2015
Easiest way:
string = 'This is a string.';
NrLtrs = length(regexpi(string, '[a-zA-Z]'));
The number of letters only is returned in the ‘NrLtrs’ variable.
  7 Comments
Star Strider
Star Strider on 24 May 2015
If it’s a cell, address it as a cell:
string = {'This is a string.'};
NrLtrs = length(regexpi(string{1}, '[a-zA-Z]'));
You will likely have to experiment with this idea in the context of your code to get the result you want.

Sign in to comment.

More Answers (2)

yashar khatib shahidi
yashar khatib shahidi on 23 May 2015
Thanks.

Jan
Jan on 24 May 2015
Or:
sum(isstrprop(string, 'alpha'))

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by