Do I have to specify a variable for each identical argument in String.format?

From the docs: The format specifiers for general, character, and numeric types have the following syntax: %[argument_index$][flags][width][.precision]conversion The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by “1$”, the second by “2$”, etc. String.format(“%1$s %1$s %1$s %1$s %1$s %1$s”, hello);

Kotlin – String formatting

Unfortunately, there’s no built-in support for formatting in string templates yet, as a workaround, you can use something like: “pi = ${pi.format(2)}” the .format(n) function you’d need to define yourself as fun Double.format(digits: Int) = “%.${digits}f”.format(this) This will work only in Kotlin/JVM. There’s clearly a piece of functionality here that is missing from Kotlin at … Read more

Formatting Numbers by padding with leading zeros in SQL Server

Change the number 6 to whatever your total length needs to be: SELECT REPLICATE(‘0’,6-LEN(EmployeeId)) + EmployeeId If the column is an INT, you can use RTRIM to implicitly convert it to a VARCHAR SELECT REPLICATE(‘0′,6-LEN(RTRIM(EmployeeId))) + RTRIM(EmployeeId) And the code to remove these 0s and get back the ‘real’ number: SELECT RIGHT(EmployeeId,(LEN(EmployeeId) – PATINDEX(‘%[^0]%’,EmployeeId)) + … Read more

What is Ruby equivalent of Python’s `s= “hello, %s. Where is %s?” % (“John”,”Mary”)`

The easiest way is string interpolation. You can inject little pieces of Ruby code directly into your strings. name1 = “John” name2 = “Mary” “hello, #{name1}. Where is #{name2}?” You can also do format strings in Ruby. “hello, %s. Where is %s?” % [“John”, “Mary”] Remember to use square brackets there. Ruby doesn’t have tuples, … Read more

Named string formatting in C#

There is no built-in method for handling this. Here’s one method string myString = “{foo} is {bar} and {yadi} is {yada}”.Inject(o); Here’s another Status.Text = “{UserName} last logged in at {LastLoginDate}”.FormatWith(user); A third improved method partially based on the two above, from Phil Haack

partial string formatting

If you know in what order you’re formatting things: s=”{foo} {{bar}}” Use it like this: ss = s.format(foo=’FOO’) print ss >>> ‘FOO {bar}’ print ss.format(bar=”BAR”) >>> ‘FOO BAR’ You can’t specify foo and bar at the same time – you have to do it sequentially.