9

I know that

parseInt(myString, 10) // "Never forget the radix"  

will return a number if the first characters in the string are numerical, but how can I do this in JavaScript if I have a string like "column5" and want to increment it to the next one ("column6")?

The number of digits at the end of the string is variable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sova
  • 5,468
  • 10
  • 40
  • 48
  • 2
    please never use parseInt without the 2nd argument, 10 here, if you do not want very strange bugs :-) – regilero Jan 11 '11 at 15:56

6 Answers6

22
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

Kai
  • 9,038
  • 5
  • 28
  • 28
  • What you do for 3 chars ? or 4 or 5 or 12 ? – Mihai Toader Jan 11 '11 at 15:55
  • I like this but I think .slice(6,string.length) will give you the last X-many digits (a la benhowdle89's comment) – sova Jan 11 '11 at 15:58
  • you don't know a priori how many "columns" (aka digits) the ending number will have – Mihai Toader Jan 11 '11 at 16:00
  • do too. all strings look like columnXXX where XXX is a variable number of digits. You know "column" is 6 characters long and you know the total length of the string columnXXX..., subtract them and you get numDigits. Equivalent to slicing off the (known) left half instead of trying to guess the size of the right. – sova Jan 11 '11 at 16:02
  • @Toader Mihai Claudiu The OP didn't specify that the digit length was arbitrary in the original question, but I've updated my answer to include that case. – Kai Jan 11 '11 at 16:20
  • @Toader Mihai Claudiu Thanks :) – Kai Jan 11 '11 at 16:29
4

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;
Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71
3

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 ;).

Mihai Toader
  • 12,041
  • 1
  • 29
  • 33
1
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

benhowdle89
  • 36,900
  • 69
  • 202
  • 331
0
  1. Split the word and number using RegEx.

  2. using parseInt() increment the number.

  3. Append to the word.

niksvp
  • 5,545
  • 2
  • 24
  • 41
0

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.

Karol
  • 7,803
  • 9
  • 49
  • 67