Can you use replace to remove all characters that aren't letters from a string?

147 visualizaciones (últimos 30 días)
I was wondering if I can use replace(str, ~['A':'Z' 'a':'z'], ' ') to replace all values that are not letters with an empty character. For example, if the string was 'I; love123 you-+'
I would need it to make a new string that is 'I love you'. Additionally, if this is not possible is there another way of doing this?

Respuesta aceptada

Steven Lord
Steven Lord el 28 de Oct. de 2019
You can use isstrprop to create a mask for various categories of characters then use that mask to extract just all letters, or just all numbers, etc.

Más respuestas (1)

Shubham Gupta
Shubham Gupta el 27 de Oct. de 2019
Editada: Shubham Gupta el 27 de Oct. de 2019
Try
str = 'I; love123 you-+';
str_new = regexprep(str,'[^a-zA-Z\s]','');
  1 comentario
Zach Adams
Zach Adams el 28 de Oct. de 2019
That worked! It will not let me accept the answer at the moment so I will keep trying. Thanks.

Iniciar sesión para comentar.

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