remove to only one whitespace in a string

25 visualizaciones (últimos 30 días)
jarvan
jarvan el 11 de Nov. de 2014
Comentada: Orion el 12 de Nov. de 2014
hi all, I am writing a function that to remove the blanks between the words and return it to only one space. like
mystr = 'Lets go party'
Return to
remove_blanks(mystr) = 'Lets go party'
So far, I got mystr = 'Lets go party'; function remove_blanks(mystr) y = mystr(isspace(mystr))='';
I know how to remove all the space between the words but I don't know how to remove to one left.

Respuesta aceptada

Orion
Orion el 11 de Nov. de 2014
Editada: Orion el 11 de Nov. de 2014
Hi,
you need to use a regular expression
mystr = 'Lets go party';
mystr = regexprep(mystr,'\s+',' ');
% \s+ means multiple spaces
mystr =
Lets go party
  3 comentarios
Guillaume
Guillaume el 12 de Nov. de 2014
It's not clear what you're asking. regexp is a function.
Orion
Orion el 12 de Nov. de 2014
just do a function :
function cleanstr = remove_blanks(mystr)
cleanstr = regexprep(mystr,'\s+',' ');

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 11 de Nov. de 2014
A regular expression is the simplest way to do that. Otherwise you can always strsplit the string (which ignores multiple spaces by default) and strjoin it back.

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