Using JavaScript's parseInt at end of string

那年仲夏 提交于 2019-12-01 17:11:31
parseInt("column5".slice(-1), 10);

You can use -1 or -2 for one to two digit numbers, respectively.

If you want to specify any length, you can use the following to return the digits:

parseInt("column6445".match(/(\d+)$/)[0], 10);

The above will work for any length of numbers, as long as the string ends with one or more numbers

Try this:

var match = myString.match(/^([a-zA-Z]+)([0-9]+)$/);
if ( match ) {
  return match[1] + (parseInt(match[2]) + 1, 10);
}

this will convert strings like text10 to text11, TxT1 to Txt2, etc. Works with long numbers at the end.

Added the radix to the parseInt call since the default parseInt value is too magic to be trusted.

See here for details:

http://www.w3schools.com/jsref/jsref_parseInt.asp

basically it will convert something like text010 to text9 which is not good ;).

Split the number from the text, parse it, increment it, and then re-concatenate it. If the preceding string is well-known, e.g., "column", you can do something like this:

var precedingString = myString.substr(0, 6); // 6 is length of "column"
var numericString = myString.substr(7);
var number = parseInt(numericString);
number++;

return precedingString + number;
var my_car="Ferrari";
var the_length=my_car.length;
var last_char=my_car.charAt(the_length-1);
alert('The last character is '+last_char+'.');

Credit to http://www.pageresource.com/jscript/jstring1.htm

Then just increment last_char

  1. Split the word and number using RegEx.

  2. using parseInt() increment the number.

  3. Append to the word.

Just try to read string char by char, checking its ASCII code. If its from 48 to 57 you got your number. Try with charCodeAt function. Then just split string, increment the number and its done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!