MATLAB Answers

Print Last Letter in a String

68 views (last 30 days)
Brandon Jennings
Brandon Jennings on 30 Jan 2015
Commented: Rik on 2 Aug 2021
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 :)

Answers (2)

Star Strider
Star Strider on 31 Jan 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 Comment
Image Analyst
Image Analyst on 31 Jan 2015
Another way to get it printed out to the command window:
fprintf('%c\n', S1(end));

Sign in to comment.


Oscar Sotomayor
Oscar Sotomayor on 31 Jul 2021
Check documentation for function extractBetween
  3 Comments
Rik
Rik on 2 Aug 2021
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'}

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!