last letter in a string

I have a string that is going to keep updating
Say x='ear' how do I take the last later in the string so say lastletter='r' is it possible to have this work generally to always take the last later in a the string so say x gets updated to 'eart' lastletter=-t'

 Respuesta aceptada

Thorsten
Thorsten el 11 de Nov. de 2015

1 voto

x = 'ear';
lastletter = x(end);

3 comentarios

ABDALHADI ABU ZEYNEH
ABDALHADI ABU ZEYNEH el 22 de Mayo de 2020
if x=dbacdcbad,how can i pick all the a's and c's so that the answer is ''acca''?
thank you in advance
Justin
Justin el 25 de Oct. de 2023
Editada: Justin el 25 de Oct. de 2023
AllA = x(x == "a")
I have not tested this, just a belief this works
Walter Roberson
Walter Roberson el 25 de Oct. de 2023
No that would not work. When you use == to compare a character vector to a string scalar, the character vector gets converted to a string scalar and then the == would be comparing for string equality not character equality.
On the other hand
x(x == 'a')
would pull out all of letter 'a'

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Max
el 11 de Nov. de 2015

Comentada:

el 25 de Oct. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by