lastChar :: String -> CharlastChar s
| s=="" = ""
| otherwise = s !! (length s - 1)
importstatic java.lang.Character.isSurrogate;
intn= s.length(), i = n - 1;
if (isSurrogate(s.charAt(i))) --i;
Stringc= s.substring(i);
"... [In Java] a String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information)."
function GetLastUtfCodePoint(const S: String): String;
var
p: PChar;
PLen: PtrInt;
begin
Result := '';
p := UTF8CodepointStart(PChar(S), Length(S), Utf8Length(S) - 1);
PLen := UTF8CodepointSize(p);
Result := p;
SetLength(Result,PLen);
end;
var
s: string;
begin
c := GetLastUtfCodePoint(s);
end.
This is a solution for UTF8 encoded strings. The type of c is String, also encoded as UTF-8. An empty string is returned if s is empty.
{$modeswitch unicodestrings}
c := s[Length(s)];
The {$modeswitch unicodestrings} makes the compiler use unicodestrings, whereas by default strings are single byte encoded. c is of type WideChar (2 bytes wide) If s is empty an exception will be raised, since the code then accesses s[0], and strings are 1-based in Pascal
$c = substr($s, -1, 1);
If $s is empty or undefined, $c is the empty string.
function GetLastUtfCodePoint(const S: String): String;
var
p: PChar;
PLen: PtrInt;
begin
Result := '';
p := UTF8CodepointStart(PChar(S), Length(S), Utf8Length(S) - 1);
PLen := UTF8CodepointSize(p);
Result := p;
SetLength(Result,PLen);
end;
var
s: string;
begin
c := GetLastUtfCodePoint(s);
end.