Getting the last character of any variable

Author Topic: Getting the last character of any variable  (Read 1456 times)

Im trying to get the last character of a variable and I can't do it.

I've been messing with get SubStr and strLen, but I can't figure it out.

Code: [Select]
%string = "hello my last character is: %";
%lastCharacter = getSubStr(%string, strLen(%string)-1, 1);

Code: [Select]
%string = "hello my last character is: %";
%lastCharacter = getSubStr(%string, strLen(%string)-1, 1);
error, starting position and desired length must be >= 0


error, starting position and desired length must be >= 0
Sounds like you didn't define %string, or you did define it but it's still empty

Code: [Select]
==>%string = "hello my last character is: %";
==>%lastCharacter = getSubStr(%string, strLen(%string)-1, 1);

getSubStr(...): error, starting position and desired length must be >= 0: ("",-1, 1)
BackTrace: ->ConsoleEntry::eval

==>echo($lastcharacter);


%string is a local variable, after you hit enter it is cleared. You must do it on 1 line, or use $string instead of %string.

Code: [Select]
==>%string = "hello my last character is: %";
==>%lastCharacter = getSubStr(%string, strLen(%string)-1, 1);

getSubStr(...): error, starting position and desired length must be >= 0: ("",-1, 1)
BackTrace: ->ConsoleEntry::eval

==>echo($lastcharacter);

Code: [Select]
==>%string = "hello my last character is: %";
==>%lastCharacter = getSubStr(%string, strLen(%string)-1, 1);

getSubStr(...): error, starting position and desired length must be >= 0: ("",-1, 1)
BackTrace: ->ConsoleEntry::eval

==>echo($lastcharacter);

Wow.
First of all, all local (%) variables are dumped at the end of a console line (not a script line or function line though).
This means that once you type %string = "hello my last character is: %"; and hit enter, %string gets deleted.
Second of all, even if the above were false and local variables remained, you're trying to echo $lastcharacter instead of %lastcharacter.

TripNick's post clearly demonstrates it working properly.