Print Last Letter in a String

조회 수: 63 (최근 30일)
Brandon Jennings
Brandon Jennings 2015년 1월 30일
댓글: Rik 2021년 8월 2일
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 :)

답변 (2개)

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

댓글을 달려면 로그인하십시오.


Oscar Sotomayor
Oscar Sotomayor 2021년 7월 31일
Check documentation for function extractBetween
  댓글 수: 3
Oscar Sotomayor
Oscar Sotomayor 2021년 8월 2일
편집: Rik 2021년 8월 2일
If x has the text. The next code extract the two lat letters
newStr = extractBetween(x,strlength(x)-1,strlength(x))
Rik
Rik 2021년 8월 2일
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'}

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by