Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
r := []rune(s)
c := r[len(r)-1]
c has type rune.
Panics if s is empty.
Panics if s is empty.
char c = s[^1];
lastChar :: String -> Char
lastChar s
| s=="" = ""
| otherwise = s !! (length s - 1)
int n = s.length(), i = n - 1;
if (isSurrogate(s.charAt(i))) --i;
String c = 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.
The type of c is String, also encoded as UTF-8.
An empty string is returned if s is empty.
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
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.