Skip to content

Instantly share code, notes, and snippets.

@SupaHam
Last active July 14, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SupaHam/e843618c441bb229ca74dedf208efe57 to your computer and use it in GitHub Desktop.
Save SupaHam/e843618c441bb229ca74dedf208efe57 to your computer and use it in GitHub Desktop.
Using Kotlin to increase pound value.
/*
* @jkbbwr provided the workaround below where you prefix the trailing s with a number such as 1.
* The number preceding the s is a padding, but if it's set to 1, there won't be any padding if your string isn't empty.
* This works because a variable cannot start with a number, so Kotlin immediately escapes $ if a number follows.
*/
fun main(args: Array<String>) {
println("""
|CREATE TABLE IF NOT EXISTS `%1$1s` `%2$1s` `%1$1s` (
| ...
|)
""".trimMargin().format("myTable", "myTable2"))
// Outputs: CREATE TABLE IF NOT EXISTS `myTable` `myTable2` `myTable` (
}
/*
* See code above for a workaround without the following.
*/
fun main(args: Array<String>) {
println("""
|CREATE TABLE IF NOT EXISTS `%1£s` (
| ...
|)
""".trimMargin().format("myTable"))
}
/**
* Increases pound value.
*/
fun String.format(vararg args: Any?) = java.lang.String.format(this.replace(Regex("""%(\d+)£s"""), "%\$1\\\$s"), *args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment