How can I find the number of letters in a string? Just letters (A-Z, a-z)?
27 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
yashar khatib shahidi
il 23 Mag 2015
Commentato: Star Strider
il 24 Mag 2015
How can I find the number of letters in a string? Just letters (A-Z, a-z)?
0 Commenti
Risposta accettata
Star Strider
il 23 Mag 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 Commenti
Image Analyst
il 23 Mag 2015
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
il 24 Mag 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.
Più risposte (2)
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!