JavaScript:to write a function which increments a string, to create a new string
2015-04-23 14:55
691 查看
Your job is to write a function which increments a string, to create a new string. If the string already ends with a number, the number should be incremented by 1. If the string does not end with a number the number 1 should be appended to the new string.
Examples:
foo -> foo1
foobar23 -> foobar24
foo0042 -> foo0043
Attention: If the number has leading zeros the amount of digits should be considered.
Examples:
foo -> foo1
foobar23 -> foobar24
foo0042 -> foo0043
Attention: If the number has leading zeros the amount of digits should be considered.
function incrementString(input) { // return incrementedString var inputLen = input.length; var lastELePos = inputLen-1; var lastEle = input.charAt(lastELePos); if (/\d/g.test(lastEle)) { // /\d/g 判断是非为数值 如果是 则运行 for(var i=0;i<inputLen;i++){ //找第一个不0的数字的位置 if(/\d/g.test(input.charAt(i)) && input.charAt(i)!=0 ){ var s1 = input.substring(0,i); var s2 = Number(input.substring(i))+1; var newStr = s1+s2; return newStr; } } }else { var newStr = input + "1"; return newStr; } } var s1 = incrementString('foobar'); var s2 = incrementString('foobar01'); var s3 = incrementString('foobar009'); alert(s1); //foobar1 alert(s2); //foobar02 alert(s3); //foobar0010
相关文章推荐
- Given the string of parentheses only, write the function to check if they are balanced. ((())) is ba
- 4、Write a function to find the longest common prefix string amongst an array of strings
- [Javascript] Different ways to create an new array/object based on existing array/object
- Write a function to find the longest common prefix string amongst an array of strings.
- Object.create() vs new SomeFunction() in javascript
- A SQL Function which used to split string by char
- Some WIX extension tags write rows to CreateFolder table which lead to Not-Uninstallable Patch
- Object.create(): the New Way to Create Objects in JavaScript
- Write a program to convert string to number without using library function。
- Write a function to convert an IPv4 Address in string format to an unsigned integer
- Write a function to find the longest common common string amongst an array of strings.
- [已解决]在VMware-server for linux 下装centos错误:an error has occurred. - no valid devices were found on which to create new file system
- Interview Q&A - Write a C# console application to implement a string replacement function
- Add new function to a prototype in javascript
- An errpr has occurred - no valid devices were found on which to create new file systems.
- JavaScript 学习笔记(一)prompt、parseInt、document.write、switch(a) {case **:……}、new Date()、getFullYear()
- openstack newton dashboard报错RuntimeError: Unable to create a new session key. It is likely that the
- 向json中添加新的熟悉或对象 Add new attribute (element) to JSON object using JavaScript
- $user_session = new \Zend\Session\Container('user'); $user_session->username = 'Andy0708'; // Write to session echo $username = $user_session->userna
- PlayFramework “unable to create new native thread”问题