Detect every second character from a string

13 visualizaciones (últimos 30 días)
Bob
Bob el 3 de Feb. de 2014
Editada: Bob el 5 de Feb. de 2014
How can I detect every second character of a string?
Thanks in advance!
  1 comentario
Thomas
Thomas el 3 de Feb. de 2014
Is this a homework problem? What have tried so far and where are you getting stuck?

Iniciar sesión para comentar.

Respuesta aceptada

Iain
Iain el 3 de Feb. de 2014
Editada: Iain el 3 de Feb. de 2014
This counts spaces & punctuation as characters (starting with the 1st character).
string_var = 'The sun is shining. I feel good.';
answer = string_var(1:2:end)
This eliminates periods:
string_var = 'The sun is shining. I feel good.';
string_var(string_var == '.') = '';
answer = string_var(1:2:end)
Change the . to whatever character you want to eliminate. You want a space.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by