R, 65 61 bytes
#R, 65 61 bytes
-2 bytes thanks to MickyT
-2 bytes by changing the indexing
returns an anonymous function.
function(s,n,x=nchar(s))`if`(n>x,'',substring(s,x:n-n+1,n:x))
substring
cycles through the indices (as opposed to substr
which does not), and if the starting index is less than 1, it defaults to 1
instead, so it checks and returns the empty string.
x:n-n+1
is equivalent to 1:(x-n+1)
since :
takes precedence over sums/differences