Last active
April 2, 2021 17:27
-
-
Save ThePyProgrammer/89a26da45bb698eda3ac2f6eb0a551fb to your computer and use it in GitHub Desktop.
StringBuilder extension with Kotlin
This file contains 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
fun StringBuilder.write( | |
vararg objects: Any?, | |
sep: String = "", | |
end: String = "", | |
prefix: String = "", | |
postfix: String = "" | |
) { | |
append(objects.joinToString(separator = sep, prefix = prefix, postfix = postfix)+end) | |
} | |
fun StringBuilder.writeln(vararg objects: Any?, sep: String = "", end: String = "", prefix: String = "", postfix: String = "") { | |
append(objects.joinToString(separator = sep, prefix = prefix, postfix = postfix)+end+"\n") | |
} | |
fun StringBuilder.writelns(vararg objects: Any?, sep: String = "", end: String = "", prefix: String = "", postfix: String = "") { | |
append(objects.joinToString(separator = sep+"\n", prefix = prefix, postfix = postfix)+end) | |
} | |
fun StringBuilder.writeElement(vararg objects: Any?, tag: String = "p", sep: String = "", end: String = "\n") { | |
append(objects.joinToString(separator = sep, prefix = "<$tag>", postfix = "</$tag>")+end) | |
} | |
fun stringBuild( | |
vararg objects: Any?, | |
sep: String = "", | |
end: String = "", | |
prefix: String = "", | |
postfix: String = "" | |
) = StringBuilder(objects.joinToString(separator = sep, prefix = prefix, postfix = postfix)+end) |
This file contains 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
/* | |
* Copyright (c) 2021 by Prannaya Gupta | |
*/ | |
class SuperStringBuilder(val builder: StringBuilder = StringBuilder()) { | |
constructor( | |
vararg objects: Any?, | |
sep: String = "", | |
end: String = "", | |
prefix: String = "", | |
postfix: String = "" | |
): this(StringBuilder(objects.joinToString(separator = sep, prefix = prefix, postfix = postfix)+end)) | |
fun write( | |
vararg objects: Any?, | |
sep: String = "", | |
end: String = "", | |
prefix: String = "", | |
postfix: String = "" | |
) { | |
builder.append(objects.joinToString(separator = sep, prefix = prefix, postfix = postfix)+end) | |
} | |
fun writeln(vararg objects: Any?, sep: String = "", end: String = "", prefix: String = "", postfix: String = "") { | |
builder.append(objects.joinToString(separator = sep, prefix = prefix, postfix = postfix)+end+"\n") | |
} | |
fun writelns(vararg objects: Any?, sep: String = "", end: String = "", prefix: String = "", postfix: String = "") { | |
builder.append(objects.joinToString(separator = sep+"\n", prefix = prefix, postfix = postfix)+end) | |
} | |
fun writeElement(vararg objects: Any?, tag: String = "p", sep: String = "", end: String = "\n") { | |
builder.append(objects.joinToString(separator = sep, prefix = "<$tag>", postfix = "</$tag>")+end) | |
} | |
override fun toString() = builder.toString() | |
} |
This file contains 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
fun makeTable(list: List<List<Any?>>, border: Double = 1.0) = run { | |
val builder = SuperStringBuilder( | |
"<table border=\"${border}\">", | |
"<tr>", | |
sep="\n" | |
) | |
list.forEach { row -> | |
row.forEach { element -> | |
builder.writeElement(element, tag="td", end="\n") | |
} | |
builder.writeln("</tr>", "<tr>", sep="\n") | |
} | |
builder.writeln("<tr>", "<table>") | |
builder.toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment