strings syntax
Import strings
syntax
import extras.strings.syntax.strings._
Or
import extras.strings.syntax.all._
commaWith(String)
An extension method for a Seq[String]
, providing a way to join the String
elements with a comma and a given
conjunction.
It joins String
values with commas and uses the given conjunction before the last element.
List.empty[String].commaWith("BLAH")
// res0: String = ""
List("").commaWith("BLAH")
// res1: String = ""
List("aaa").commaWith("BLAH")
// res2: String = "aaa"
List("aaa", "bbb").commaWith("BLAH")
// res3: String = "aaa BLAH bbb"
List("aaa", "bbb", "ccc").commaWith("BLAH")
// res4: String = "aaa, bbb BLAH ccc"
List("aaa", "bbb", "ccc", "ddd").commaWith("BLAH")
// res5: String = "aaa, bbb, ccc BLAH ddd"
serialCommaWith(String)
An extension method for a Seq[String]
, providing a way to join the String
elements with a serial comma and a given
conjunction.
It joins String values with commas and uses the given conjunction before the last element.
This method employs the serial comma (also known as the Oxford comma), which means it always inserts a comma before the conjunction unless there are only two elements.
List.empty[String].serialCommaWith("BLAH")
// res6: String = ""
List("").serialCommaWith("BLAH")
// res7: String = ""
List("aaa").serialCommaWith("BLAH")
// res8: String = "aaa"
List("aaa", "bbb").serialCommaWith("BLAH")
// res9: String = "aaa BLAH bbb"
List("aaa", "bbb", "ccc").serialCommaWith("BLAH")
// res10: String = "aaa, bbb, BLAH ccc"
List("aaa", "bbb", "ccc", "ddd").serialCommaWith("BLAH")
// res11: String = "aaa, bbb, ccc, BLAH ddd"
commaAnd
Format Seq[String]
into a human-readable list using comma and the conjunction and
.
It separates elements by commas and uses the term and
before the last element.
List.empty[String].commaAnd
// res12: String = ""
List("").commaAnd
// res13: String = ""
List("aaa").commaAnd
// res14: String = "aaa"
List("aaa", "bbb").commaAnd
// res15: String = "aaa and bbb"
List("aaa", "bbb", "ccc").commaAnd
// res16: String = "aaa, bbb and ccc"
List("aaa", "bbb", "ccc", "ddd").commaAnd
// res17: String = "aaa, bbb, ccc and ddd"
serialCommaAnd
Format Seq[String]
into a human-readable list using comma and the conjunction and
.
It separates elements by commas and uses the term and
before the last element following the "Oxford comma" style. e.g.) "aaa, bbb, and ccc"
List.empty[String].serialCommaAnd
// res18: String = ""
List("").serialCommaAnd
// res19: String = ""
List("aaa").serialCommaAnd
// res20: String = "aaa"
List("aaa", "bbb").serialCommaAnd
// res21: String = "aaa and bbb"
List("aaa", "bbb", "ccc").serialCommaAnd
// res22: String = "aaa, bbb, and ccc"
List("aaa", "bbb", "ccc", "ddd").serialCommaAnd
// res23: String = "aaa, bbb, ccc, and ddd"
commaOr
Format Seq[String]
into a human-readable list using comma and the conjunction or
.
It separates elements by commas and uses the term or
before the last element.
List.empty[String].commaOr
// res24: String = ""
List("").commaOr
// res25: String = ""
List("aaa").commaOr
// res26: String = "aaa"
List("aaa", "bbb").commaOr
// res27: String = "aaa or bbb"
List("aaa", "bbb", "ccc").commaOr
// res28: String = "aaa, bbb or ccc"
List("aaa", "bbb", "ccc", "ddd").commaOr
// res29: String = "aaa, bbb, ccc or ddd"
serialCommaOr
Format Seq[String]
into a human-readable list using comma and the conjunction or
.
It separates elements by commas and uses the term or
before the last element following the "Oxford comma" style. e.g.) "aaa, bbb, or ccc"
List.empty[String].serialCommaOr
// res30: String = ""
List("").serialCommaOr
// res31: String = ""
List("aaa").serialCommaOr
// res32: String = "aaa"
List("aaa", "bbb").serialCommaOr
// res33: String = "aaa or bbb"
List("aaa", "bbb", "ccc").serialCommaOr
// res34: String = "aaa, bbb, or ccc"
List("aaa", "bbb", "ccc", "ddd").serialCommaOr
// res35: String = "aaa, bbb, ccc, or ddd"