How can I find the number of letters in a string? Just letters (A-Z, a-z)?
조회 수: 1 (최근 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!