This language bar is your friend. Select your favorite languages!

Idiom #47 Extract string suffix

Create string t consisting in the 5 last characters of string s.

Illustration
#include <stdlib.h>
#include <string.h>
int len=strlen(s);
char *t=malloc(6*sizeof(char));
strncpy(t,s+len-6,5);
(let [t (clojure.string/join (take-last 5 s))])
IDENTIFICATION DIVISION.
PROGRAM-ID. suffix.
PROCEDURE DIVISION.
    MOVE FUNCTION LENGTH(s) TO len
    COMPUTE pos = (len - 5) + 1
    MOVE    s(pos:) TO t    	 	
STOP RUN.
#include <string>
// std::string s;
auto suffix = s.substr(s.length() - 5, 5);
var t = s.Substring(s.Length - 5);
string t = s[$-5..$];
var n = s.length;
var t = s.substring(n-5, n);
t = String.slice(s, -5, 5)
[T5, T4, T3, T2, T1 | _] = lists:reverse(S),
T = [T1, T2, T3, T4, T5].
character(len=5) :: t
t = s(len(s)-4:)
t := string([]rune(s)[len([]rune(s))-5:])
t = drop (length s - 5) s
var t = s.slice(-5);
int n = s.length();
String t = s.substring(n-5,n);
(setf *t* (subseq s (- (length s) 5)))
t = s:sub(-5)
$t = mb_substr($s, -5, null, 'UTF-8');
Function RightStr(const AText: AnsiString; const ACount: Integer): AnsiString;
var j,l:integer;
begin
  l:=length(atext);
  j:=ACount;
  if j>l then j:=l;
  Result:=Copy(AText,l-j+1,j);
end;
my $last_five = substr("now is the time", -5);
t = s[-5:]
t=s[-5..-1]
let t = s[s.len() - 5..].to_string();
Dim s As String = "MY STRING"
Dim t As String
t = s.Substring(s.Length - 5)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms