Get Last Letter or number in string
#1

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'
Reply
#2

string[sizeof(string) - 1]
Reply
#3

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?
Reply
#4

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
Reply
#5

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
Reply
#6

Why %c you are using?
Reply
#7

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)