How can I find the number of letters in a string? Just letters (A-Z, a-z)?
27 次查看(过去 30 天)
显示 更早的评论
How can I find the number of letters in a string? Just letters (A-Z, a-z)?
0 个评论
采纳的回答
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
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
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!