3

Let's say I have the following string:

aRandomString

I'm then checking my database to see if that string has been used before (or if there's any string that starts with the given one). If it has, I'd like to increment it. The result would be the following:

aRandomString1

If that string exists, it would become aRandomString2, and so on. I'm assuming I need to complete the following steps: 1. Check for trailing numbers or set to 0, 2. Increment those trailing numbers by 1, append that number to the original string. Just trying to figure out a solid way to accomplish this!

Update

This is what I have so far:

var incrementString = function( str ){
    var regexp = /\d+$/g;
    if( str.match(regexp) )
    {
        var trailingNumbers = str.match( regexp )[0];
        var number = parseInt(trailingNumbers);
        number += 1;

        // Replace the trailing numbers and put back incremented
        str = str.replace(/\d+$/g , '');
        str += number;
    }else{
        str += "1";
    }
    return str;
}

I feel like this should be easier though.

10
  • 1
    Do you have an attempt at the algorithm you outlined? Commented Jan 14, 2014 at 19:34
  • The only numbers in your string will be at the end of it? Commented Jan 14, 2014 at 19:35
  • @Felipe, no, there could be numbers elsewhere. Commented Jan 14, 2014 at 19:36
  • @ajp15243 not yet. Will post an attempt momentarily. Commented Jan 14, 2014 at 19:36
  • @NickONeill If there can be numbers in other parts of the string, then you should probably choose a character unique from any other characters that the string can contain, and use that to separate your string from the incremental number at the end. That way you won't confuse it if you have a string that, without the incrementing number, is of the form x123 (where 123 can be confused with the incrementing number that you'll stick on the end). Commented Jan 14, 2014 at 19:43

3 Answers 3

9
function incrementString(str) {
  // Find the trailing number or it will match the empty string
  var count = str.match(/\d*$/);

  // Take the substring up until where the integer was matched
  // Concatenate it to the matched count incremented by 1
  return str.substr(0, count.index) + (++count[0]);
};

If the match is the empty string, incrementing it will first cast the empty string to an integer, resulting the value of 0. Next it preforms the increment, resulting the value of 1.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, this was incredibly elegant!
But in the case of input string with zero prefix e.g. 001 not working properly...
updated @adamduren 's answer to handle leading zeros, below is for number with 3 digits, e.g. 001 return str.substr(0, count.index) + ("000" + (++count[0])).slice(-3);
5

I had to keep the leading zeros in the number, this is how I did it:

function incrementString (string) {  
  // Extract string's number
  var number = string.match(/\d+/) === null ? 0 : string.match(/\d+/)[0];
  
  // Store number's length
  var numberLength = number.length

  // Increment number by 1
  number = (parseInt(number) + 1).toString();
  
  // If there were leading 0s, add them again
  while (number.length < numberLength) {
    number = "0" + number;
  }
    
  return string.replace(/[0-9]/g, '').concat(number);
}

Comments

0

Incrementing string with support for padding zeros A001 will become A002. And support for plain string ABC will become ABC1. And adding digits when needed: ABC9 will become ABC10. I've used simular function for invoice number incremention.

  const incrementNr = (nr) => {
    const [nrDigits] = /\d*$/.exec(nr);
    const next = '' + (+nrDigits + 1);
    const nextNr = nr.slice(0, nr.length - Math.min(next.length, nrDigits.length)) + next;
    return nextNr;
  };```

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.