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

27 visualizaciones (últimos 30 días)
How can I find the number of letters in a string? Just letters (A-Z, a-z)?

Respuesta aceptada

Star Strider
Star Strider el 23 de Mayo de 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 comentarios
Image Analyst
Image Analyst el 23 de Mayo de 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
Star Strider el 24 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (2)

yashar khatib shahidi
yashar khatib shahidi el 23 de Mayo de 2015
Thanks.

Jan
Jan el 24 de Mayo de 2015
Or:
sum(isstrprop(string, 'alpha'))

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by