SA-MP Forums Archive
Get Last Letter or number in string - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Get Last Letter or number in string (/showthread.php?tid=549328)



Get Last Letter or number in string - Banditukas - 06.12.2014

Hi,

How i can to get a last letter or number in string for ex:

:asd2ASwqeV - in this string i need to get that last letter is 'V'
KgwerrwerWgfdgdfgdfgH - in this string i need to get that last letter is 'H'
KlwerwerdsZSDfwererW - in this string i need to get that last letter is 'W'
Vm,zsdqweasd5 - in this string i need to get that last number is '5'


Re: Get Last Letter or number in string - AnthonyTimmers - 06.12.2014

string[sizeof(string) - 1]


Re: Get Last Letter or number in string - Banditukas - 06.12.2014

But if i have that variable:

new Variable[ 50 ];

And then i format:

format(Variable,49,"vas5DwelresdQ" );

How then? is that will be still working?


Re: Get Last Letter or number in string - Virtual1ty - 06.12.2014

Actually Anthony, that is wrong. Let's say that "string[50]", now all of the indexes (0 to 49) might not contain a character ie. they might be NULL.

This is the correct way:
pawn Код:
new len = strlen(string); // If you want to store the length for later use
printf("%c", string[len - 1]); // Prints the last character of the string



Re: Get Last Letter or number in string - AnthonyTimmers - 06.12.2014

Quote:
Originally Posted by Banditukas
Посмотреть сообщение
But if i have that variable:

new Variable[ 50 ];

And then i format:

format(Variable,49,"vas5DwelresdQ" );

How then? is that will be still working?
No, I thought you'd be using it on (randomly) fully filled strings.

If this isn't the case, you might use strlen - 1 as the index to find the last value. For syntax: https://sampwiki.blast.hk/wiki/Strlen


Re: Get Last Letter or number in string - Banditukas - 06.12.2014

Why %c you are using?


Re: Get Last Letter or number in string - Virtual1ty - 06.12.2014

Read the documentation on printf. I am printing a single character therefore I used the '%c' specifier. If you were to use '%s' it wouldn't work as planned as it expects a NULL terminating that "string" somewhere.