-2

The R is displaying last in the string initially it must display first how can I put the last character before the number value in the string so that it can display at the beginning of the value

enter image description here

contentHtml += 
    "<td rowspan1=\"" + 1 + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || value.length < 7 && value.toString().substr(value.length - 1) == '%') ? "text-center" : "text-left") + "\">" + value + (!isNaN(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
if (rowspan > 1) {
    var rowspanContent = "<td rowspa1=\"" + rowspan + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || value.length < 7 && value.toString().substr(value.length - 1) == '%') ? "text-center" : "text-left") + "\">" + value + (!isNaN(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571

4 Answers4

2

If you want o swap the numbers and letters you should see Nina Scholz answer.

If you just want to put the last character first you can do it like this:

function lastToFirst(value){
   return value.slice(-1) + value.substring(0,value.length-1);
}

or even cleaner

function lastToFirst(value){
   return value.slice(-1) + value.slice(0,-1);
}
Cruz
  • 601
  • 1
  • 7
  • 13
1

You could use a regular expression and swap numbers and letters (not numbers).

function swap(s) {
    var m = s.match(/^(\d+)(\D+)$/);
    return m[2] + m[1];
}

console.log(swap('1234R'));

Otherwise, you could change your code from

value + (!isNaN(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "")
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                              

to

(!isNaN(value) ? preFix : "") + value + color + (!isNaN(value) ? postFix : "")
//^^^^^^^^^^^^^^^^^^^^^^^^^^^

and use preFix instead of postFix.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Direct answer is:

var value ="1234567R";
var res = "R" + value.split("R")[0];
alert(res);
Banzay
  • 9,310
  • 2
  • 27
  • 46
0

Without code, it's compicated to help. If just you want to put the last x chars to first, here is a little piece of code

var nb = 1;
var s = "123456789R";
var last = s.split(s[s.length - (nb - 1)])[1];
var s1 = s.slice(0, s.length - nb);
s = last + s1;
//s => "R123456789"
Zakawa
  • 132
  • 1
  • 5