Print Last Letter in a String

Im trying to print out the last Alphabetic letter in a string.
For example S1 = '%@3Gb6kl@3G9@33G';
I want the letter G to get printed out.
Any help would be great :)

Respuestas (2)

Star Strider
Star Strider el 31 de En. de 2015
Your ‘S1’ is a string array, so you can use the end function to get the last letter:
S1 = '%@3Gb6kl@3G9@33G';
LastLetter = S1(end)
produces:
LastLetter =
G

1 comentario

Another way to get it printed out to the command window:
fprintf('%c\n', S1(end));

Iniciar sesión para comentar.

Oscar Sotomayor
Oscar Sotomayor el 31 de Jul. de 2021

0 votos

Check documentation for function extractBetween

3 comentarios

Rik
Rik el 31 de Jul. de 2021
@Oscar Sotomayor How exactly would that help? How would you use extractBetween to extract the last element of a char vector? (or even the last character from a string scalar)
Oscar Sotomayor
Oscar Sotomayor el 2 de Ag. de 2021
Editada: Rik el 2 de Ag. de 2021
If x has the text. The next code extract the two lat letters
newStr = extractBetween(x,strlength(x)-1,strlength(x))
The question was about only the last letter, so you need a minor edit:
x="%@3Gb6kl@3G9@33G";
newStr = extractBetween(x,strlength(x),strlength(x))
newStr = "G"
x=char(x);
newStr = extractBetween(x,strlength(x),strlength(x))
newStr = 1×1 cell array
{'G'}

Iniciar sesión para comentar.

Categorías

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

Preguntada:

el 30 de En. de 2015

Comentada:

Rik
el 2 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by