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

29 ビュー (過去 30 日間)
yashar khatib shahidi
yashar khatib shahidi 2015 年 5 月 23 日
コメント済み: Star Strider 2015 年 5 月 24 日
How can I find the number of letters in a string? Just letters (A-Z, a-z)?

採用された回答

Star Strider
Star Strider 2015 年 5 月 23 日
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 件のコメント
Image Analyst
Image Analyst 2015 年 5 月 23 日
Why are you using textscan to simply get a string. That's too overkill for a simple case like this. Simply use fgetl() if you want to get line by line, or use fread() if you want to suck up the whole file in one shot.
Star Strider
Star Strider 2015 年 5 月 24 日
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.

サインインしてコメントする。

その他の回答 (2 件)

yashar khatib shahidi
yashar khatib shahidi 2015 年 5 月 23 日
Thanks.

Jan
Jan 2015 年 5 月 24 日
Or:
sum(isstrprop(string, 'alpha'))

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by