How to take last character from a string and add it to a first character of a string?

0 votes
asked Sep 13, 2017 by krittiya-clark

What solution for take last character from a string and add it to a first character of a string? It should return new string.

This what I had -->

var result = '';
var string = 'something';

var remove = string.split('');
var joinStaring = remove.pop().join('');

console.log(joinStaring);

4 Answers

0 votes
answered Sep 13, 2017 by jonas-w
const result = string.substr(-1) + string.substr(0,string.length - 1);
0 votes
answered Sep 13, 2017 by farhad-bagherlo

var str ='testString';
console.log(str.slice(-1) + str.slice(0,-1));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
0 votes
answered Sep 13, 2017 by shree

Get last character and concat. Use slice to remove last character.

var str = 'test_A',
str =str.substr(str.length - 1) + str.slice(0,-1);
alert(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
0 votes
answered Sep 13, 2017 by andre

A short way is the String.prototype.slice method. Just by:

var result = string.slice(-1);

result = result + string;

    

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...