How do i add this

I hope someone posts a solution in Erlang, zomg

How about haskell?


import Data.List.Split

padL :: Int -> String -> String
padL n s
    | length s < n = replicate (n - length s) '0' ++ s
    | otherwise = s

strInc :: String -> String -> String
strInc sep str = 
        let strarr = splitOn sep str
            zprefix = strarr !! 0
            znumber = strarr !! 1
        in zprefix ++ sep ++ padL ( length (znumber) ) ( show ( read ( znumber ) + 1 ) )

Prelude> :l strinc
[1 of 1] Compiling Main ( strinc.hs, interpreted )
Ok, modules loaded: Main.
*Main> strInc “-” “xs-00009”
“xs-00010”
*Main> strInc “<>” “yxs<>000099”
“yxs<>000100”

As with my javascript version, the length or the prefix are not hardcoded.

hello,

From my point of view the best way to do this if xs- is a constant

$number = $number++;
$var = "xs-".$number;

if you have the xs-0001 in a strig you can do

$number = explode("-","xs-0001")
$number = $number[1];

And then you can add the ++ line to add a number to the result.

Hope it helps

Really several php solutions are already provided including mine which would account for any prefix and handles floating zeros. The boys are just playing around with other coding languages.

How about Go(lang)?

I’m sure that my Haskell and Go examples can be further optimized, but so far JavaScript seems to be the winner.


list($prefix, $serial) = explode('-', $inputString);
$outputString = $prefix . '-' . str_pad($serial + 1, strlen($serial), '0', STR_PAD_LEFT);

I tend to think sprintf/printf is pretty unreadable and impenetrable at times, and anything where you can break it by changing the format string (ie, so it expects another parameter that you don’t pass in) is bad in my book. str_pad makes it clear what your intentions are and why you are doing it, so wins in my book!

I like that one Stormrider!

I like your solution too Stormrider :slight_smile:

I just realized that you can do this in Ruby:

serial = "xs-00001"
new_serial = serial.next
puts new_serial
=> "xs-00002"

which is pretty neat when you think about it.

It also deals with “xs-99999”, outputting “xs-100000”

Also, here’s a solution in Scala (which the editor doesn’t seem to have a code tag for):

def nextStr(s: String) = {
  val PrefixZerosDigits = """(xs-)(0*)(\\d+)""".r
  val Nines = "9*".r
  
  s match {
    case PrefixZerosDigits(prefix, zs, ds) => 
      val dsInc = (ds.toInt + 1).toString
      ds match {
          case Nines() => 
            if (zs.length == 0)
              prefix + dsInc
            else
              prefix + zs.drop(1) + dsInc
          case _ => prefix + zs + dsInc
      }
    case _ => throw new RuntimeException("wrong format")
  }
}

println(nextStr("xs-00001"))
=> "xs-00002"

Cool, I don’t think it could get any simpler than that… Ruby seems to have some nice functionality built-in.

It’s also very interesting to see the code from the more unusual languages like Scala and Haskel.

Edit: I also thought Stormrider’s use of the list function was pretty good, I don’t think I’ve ever used it in any code but it makes for a neat solution here.

for($i = strlen($str) - 1; $i > 2; $i--)
{
	if($str{$i} < 9)
	{
		$str{$i} = $str{$i} + 1;
		break;
	}
	$str{$i} = 0;
}