Reputation: 2000
It seems like an easy problem, but i can't find solution. I want to take first, let's say 2 letters from string, and move them to the end of this string. So for example, OK12 would become 12OK.
edit: So far i've tried cutting string off, then adding it to the rest of the string, but i tought there's a one-line solution for that, like predefined function or something.
Upvotes: 14
Views: 26704
Reputation: 303215
Various techniques:
str.slice(2) + str.slice(0,2);
str = str.replace(/^(.{2})(.+)/, '$2$1');
for (var a=str.split(""),i=2;i--;) a.push(a.shift());
str = a.join('');
Upvotes: 8
Reputation: 150253
"OK12".substr(2) + "OK12".substr(0,2)
Generic solution:
var result = str.substr(num) + str.substr(0, num);
Upvotes: 18