g | x | w | all
Bytes Lang Time Link
002UiuaSBCS240808T191159ZEurope20
nan230402T162340ZThe Thon
035Python230402T161715ZThe Thon
003Jelly230402T161212ZThe Thon
001Vyxal230402T155658ZThe Thon
001vemf230402T043928Zla.evis.
040Wolfram Language Mathematica230331T001542Z138 Aspe
004HP‑41C series230319T143648ZKai Burg
028Ruby221026T151455ZJordan
00305AB1E180801T100514ZKevin Cr
120Pascal ISO standard 10206221025T211420ZKai Burg
088PHP>=7.1170521T170234ZJör
026Perl 5 + palF180801T160145ZDom Hast
002Stax180801T140657Zwastl
031Julia 0.6180801T110609ZSundar R
027Casio Basic170521T083333Znumberma
002Japt170522T134900ZShaggy
167Java150602T015346ZJack Amm
180rs150531T000245Zkirbyfan
007K150530T142620ZJohnE
052Javascript ES5150530T162655Znderscor
045Python 3150530T144351ZDoorknob
093C150530T110638ZCL-
010Pip150530T093840ZDLosc
004Pyth150530T090629ZOptimize
005CJam150530T085757ZOptimize

UiuaSBCS, 2 bytes

↻¯

Try it here!

↻ rotate shifts cyclically to the right, so a ¯ negate has to be added to reverse the direction.

Thunno J, \$ 8 \log_{256}(96) \approx \$ 6.58 bytes

LR_z0sAI

Attempt This Online!

No shift operator in Thunno so we have to do it ourselves.

Explanation

LR_z0sAI  # Implicit input        STACK: "Hello, World!", 5
LR        # Length range          STACK: 5, [0..12]
  _       # Swapped subtract      STACK: [-5..7]
   z0     # First input           STACK: [-5..7], "Hello, World!"
     sAI  # Swap and index        STACK: ['o', 'r', 'l', 'd', '!', 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W']
          # J flag joins          STACK: "orld!Hello, W"
          # Implicit output

Python, 35 bytes

lambda s,n:s[(x:=-n%len(s)):]+s[:x]

Attempt This Online!

Jelly, 3 bytes

N⁴ṙ

Try it online!

Explanation

N⁴ṙ  # Main link. Takes an integer on the
     # left and a string on the right.
 ⁴   # Get the right argument (the string)
  ṙ  # And rotate it left by ↓ spaces
N    # The left argument negated
     # Implicit output

Vyxal, 1 byte

ǔ

Try it Online!

Takes the number then the string. Prepend $ if you want the inputs the other way round.

Built-in solution for "rotate right". Rotation is modular so we don't have to worry about negative inputs.

vemf, 1 byte

+

Left argument is string, right argument is offset.

Wolfram Language (Mathematica), 40 bytes

StringJoin@RotateRight[Characters@#,#2]&
StringJoin@RotateLeft[Characters@#,-#2]&

Try it online!

HP‑41C series, 4 Bytes

Input string is located in alpha register and number to rotate on top of the stack (i. e. the X register). An extended functions module or HP‑41CX is required.

CHS     1 Byte   flip sign because AROT acts just the opposite as required
AROT    2 Bytes  rotate alpha register according to value in X register
AVIEW   1 Byte   display contents of alpha register

Ruby, 28 bytes

->s,n{s.chars.rotate(-n)*""}

Attempt This Online!

05AB1E, 6 5 3 bytes

(._

Inputs in the order integer,string.

Try it online or verify all test cases.

Explanation:

(    # Negate the first (implicit) input-integer
 ._  # Rotate the second (implicit) input-string that many times towards the left
     # (which supports negative integers as rotating right instead)
     # (after which the result is output implicitly)

05AB1E (legacy), 6 5 bytes

sg+FÁ

Inputs in the order integer,string.

Try it online or verify all test cases.

Explanation:

s      # Swap so the two (implicit) inputs are in reversed order on the stack
 g     # Pop and push the length of the top input-string
  +    # Add it to the input-integer
   F   # Loop that many times
    Á  #  And rotate once towards the right during every iteration
       #  (using the last implicit input-string in the first iteration)

Since the legacy version of 05AB1E only has builtins for Rotate once towards the right/left, and not Rotate \$n\$ amount towards the right/left, I loop \$length + input\$ amount of times and rotate that many times towards the right.

For example:

The new version of 05AB1E does have a builtin for rotating \$n\$ amount of times towards the left, which is ._, saving 2 bytes.

Pascal (ISO standard 10206, “Extended Pascal”), 120 Bytes

program p(input,output);var s:string(100);n:integer;begin read(s,n);n:=(-n)mod length(s);writeLn(subStr(s,n+1),s:n)end.

Ungolfed:

program shiftCharactersInAString(input, output);
var
    line: string(100);
    n: integer;
begin
    read(line, n);
    { In Pascal, the result of the `mod` operation is non-negative. }
    n := (-n) mod length(line);
    { In Pascal, string indices are 1‑based. }
    writeLn(subStr(line, n + 1), line:n)
    { `line:n` is equivalent to `subStr(line, 1, n)` (if `n` ≤ `length(line)`). }
end.

PHP>=7.1, 88 Bytes

for([,$s,$t]=$argv;$t;)$s=$t<0?substr($s,1).$s[!$t++]:$s[-1].substr($s,!$t--,-1);echo$s;

Testcases

Perl 5 + -palF, 26 bytes

$_=substr$_.$_,@F-<>%@F,@F

Try it online!

Stax, 2 bytes

|)

Run and debug it

Julia 0.6, 31 bytes

s|n=String(circshift([s...],n))

Try it online!

Casio Basic, 27 bytes

StrRotate s,s,-n:Print s

As it turns out, there's a built-in for this on the Casio ClassPad! But it works in reverse, hence -n.

24 bytes for the code, 3 bytes to specify s,n as arguments.

Japt, 2 bytes

éV

Try it online

Java, 167

enum S{;public static void main(String[]r){int n=-Integer.parseInt(r[1]),l=r[0].length();while(n<0)n+=l;n%=l;System.out.print(r[0].substring(n)+r[0].substring(0,n));}}

Takes the input through the command line.

funny enough, originally I had accidentally reversed how the string was supposed to be shifted. But fixing that mistake was shorter to just multiply n by -1 then to write the logic properly.

expanded:

enum Shift{
    ;
    public static void main(String[]args){
        int n=-Integer.parseInt(args[1]),length=args[0].length();
        while(n<0)n+=length;
        n%=length;
        System.out.print(args[0].substring(n)+args[0].substring(0,n));
    }
}

rs, 180 chars

^(-\d+) (.*)/\1 \2\t
+^(-\d+) (.)(.*?)\t(.*)$/\1 \3\t\2\4
^(-\d+) \t/\1 
^(-?)(\d+)/\1 (_)^^(\2)
+_(_*) (.*)(.)$/\1 \3\2
^- /- \t
+^- (.*?)\t(.*?)(.)$/- \1\3\t\2
^-? +/
\t/

Live demo.

Most of this is reversing the string if the input number is negative. I took advantage of the fact that only some ASCII characters are valid input and used the tab to my advantage.

Note that I had to cheat a little: since rs is a single-line text modifier, I had to use <number> <text> as the input format.

K, 8 7 bytes

{|x!|y}

There is already a primitive "rotate" (!) which performs a generalization of this operation for lists. K strings are lists of characters, so it applies. The spec favors CJam and Pyth a bit, though, because K's rotate happens to go the opposite direction of what is desired. Wrapping ! in a function and negating the implicit argument x will do what we want:

  f:{(-x)!y}
{(-x)!y}
  f[5;"Hello world!"]
"orld!Hello w"
  f[-3;"Testing..."]
"ting...Tes"
  f[17;"ABA"]
"BAA"

A slightly shorter approach, suggested by kirbyfan64sos, is to do away with the parentheses and negation in favor of reversing the string (|) before and after the rotation.

If it weren't for this impedance mismatch, the solution would be simply

!

Called identically:

  f:!
!
  f[5;"Hello, World!"]
", World!Hello"
  f[-5;"Hello, World!"]
"orld!Hello, W"
  f[0;"Hello, World!"]
"Hello, World!"

Javascript (ES5), 55 52 bytes

p=prompt;with(p())p(slice(b=-p()%length)+slice(0,b))

Commented:

p = prompt; // store a copy of prompt function for reuse
with(p()) // extend scope chain with first input
    p( // print result
        slice(b = -p() % length) // take second input negated and modulo length
        +                        // and slice string by result
        slice(0, b) // concatenate with opposite slice
    )

Python 3, 45 bytes

s=input();n=int(input());print(s[-n:]+s[:-n])

The core of the program is

s[-n:]+s[:-n]

All the rest is just clumsy work with I/O.

C, 93 bytes

main(a,v,n)char**v;{a=v[2]-v[1]-1;n=atoi(v[2]);a=a*(n>0)-n%a;printf("%s%.*s",v[1]+a,a,v[1]);}

More clear is the function-argument version that was modified to make the command line-argument version

f(s,n,c)char*s;{c=strlen(s);c=c*(n>0)-n%c;printf("%s%.*s",s+c,c,s);}

This one is only 68 bytes, which just goes to show how disadvantaged C is when dealing with command line arguments.

If the shift, n, is positive then strlen(s)-n%strlen(s) is the offset and if n is negative the offset is -n%strlen(s). The printf prints from the offset, c, to the end of the string, and then the final c characters from the beginning.

Examples:

$ ./rotstr "Hello world!" 5
orld!Hello w
$ ./rotstr "Testing..." -3
ting...Tes
$ ./rotstr "~~~" 1000
~~~
$ ./rotstr "12345" 0
12345
$ ./rotstr "ABA" 17
BAA
$ ./rotstr "Hello world!" -16
o world!Hell

Pip, 10 bytes

This could quite possibly be improved further. Still, for a language with no shift operator, 10 bytes ain't bad.

a@_M-b+,#a

Explanation:

            a, b are command-line args (implicit)
       ,#a  range(len(a))
    -b+     range(-b, len(a)-b)
a@_M        map(lambda x: a[x], range(-b, len(a)-b))
            Concatenate the list and print (implicit)

It works because string and list indexing in Pip is cyclical: "Hello"@9 == "Hello"@4 == "o".

Pyth, 4 bytes

.>zQ

This is almost similar to my CJam 5 byte version, except that Pyth as a auto-eval input operator Q.

.>              # Cyclic right shift of 
  z             # Input first line as string
   Q            # Rest of the input as evaluated integer

Try it online here

CJam, 5 bytes

llim>

This is pretty straight forward.

l               e# Read the first line
 li             e# Read the second line and convert to integer
   m>           e# Shift rotate the first string by second integer places

Try it online here