Last active
July 14, 2016 13:27
-
-
Save SupaHam/e843618c441bb229ca74dedf208efe57 to your computer and use it in GitHub Desktop.
Using Kotlin to increase pound value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @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