Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://www.google.com/search?q=ruby+string+succ+JavaScript" rel="nofollow">A google search for "Ruby string succ Javascript"</a> returns <a href="https://gist.github.com/devongovett/1081265" rel="nofollow">this gist from Devon Govett called "An implementation of Ruby's string.succ method in JavaScript"</a> which appears to be what you're after...</p> <pre><code>/* * An implementation of Ruby's string.succ method. * By Devon Govett * * Returns the successor to str. The successor is calculated by incrementing characters starting * from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the * string. Incrementing a digit always results in another digit, and incrementing a letter results in * another letter of the same case. * * If the increment generates a carry, the character to the left of it is incremented. This * process repeats until there is no carry, adding an additional character if necessary. * * succ("abcd") == "abce" * succ("THX1138") == "THX1139" * succ("&lt;&lt;koala&gt;&gt;") == "&lt;&lt;koalb&gt;&gt;" * succ("1999zzz") == "2000aaa" * succ("ZZZ9999") == "AAAA0000" */ function succ(input) { var alphabet = 'abcdefghijklmnopqrstuvwxyz', length = alphabet.length, result = input, i = input.length; while(i &gt;= 0) { var last = input.charAt(--i), next = '', carry = false; if (isNaN(last)) { index = alphabet.indexOf(last.toLowerCase()); if (index === -1) { next = last; carry = true; } else { var isUpperCase = last === last.toUpperCase(); next = alphabet.charAt((index + 1) % length); if (isUpperCase) { next = next.toUpperCase(); } carry = index + 1 &gt;= length; if (carry &amp;&amp; i === 0) { var added = isUpperCase ? 'A' : 'a'; result = added + next + result.slice(1); break; } } } else { next = +last + 1; if(next &gt; 9) { next = 0; carry = true } if (carry &amp;&amp; i === 0) { result = '1' + next + result.slice(1); break; } } result = result.slice(0, i) + next + result.slice(i + 1); if (!carry) { break; } } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload