本文介绍了在字符串末尾使用JavaScript的parseInt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道
parseInt(myString, 10) // "Never forget the radix"
如果字符串中的第一个字符是数字,则会返回一个数字,但如果我有一个字符串,怎么能在JavaScript中执行此操作比如column5并希望将它增加到下一个(column6)?
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.
推荐答案
parseInt("column5".slice(-1), 10);
您可以分别使用-1或-2表示一到两位数字。
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
这篇关于在字符串末尾使用JavaScript的parseInt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!