Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move n characters from front of string to the end

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.

like image 362
Malyo Avatar asked May 31 '12 21:05

Malyo


1 Answers

"OK12".substr(2) + "OK12".substr(0,2)

Generic solution:

var result = str.substr(num) + str.substr(0, num);

Live DEMO

like image 147
gdoron is supporting Monica Avatar answered Sep 22 '22 07:09

gdoron is supporting Monica