Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:28
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 dacr/3b592b9f9ed0b88a7236503f075b8f89 to your computer and use it in GitHub Desktop.
Save dacr/3b592b9f9ed0b88a7236503f075b8f89 to your computer and use it in GitHub Desktop.
Advanced operations on strings / published by https://github.com/dacr/code-examples-manager #61e8e69d-edf9-421b-b56c-8a2da754a8d3/beed2dc7773943af7ca71dc43c6093f430a05d53
// summary : Advanced operations on strings
// keywords : scala, scalatest, strings, cheatsheet, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 61e8e69d-edf9-421b-b56c-8a2da754a8d3
// created-on : 2020-05-31T21:54:52+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.3.0"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using objectWrapper
// ---------------------
import org.scalatest._
import flatspec._
import matchers._
import OptionValues._
import java.util.Locale
import java.text.{DecimalFormat, NumberFormat}
class AdvancedStringOperations extends AnyFlatSpec with should.Matchers {
override def suiteName="AdvancedStringOperations"
// ---------------------------------------------------------------------------------------------
"interpolators" should "allow advanced string formatting" in {
Locale.setDefault(Locale.US) // TODO - find better alternative
val m="machin"
val x=42
val bx=42042
val f=42.42d
val bf=2442.42d
val d=0.42d
s"truc-$m" shouldBe "truc-machin"
raw"truc-$x\nsameline" shouldBe """truc-42\nsameline"""
raw"truc-$x\nsameline" shouldBe "truc-42\\nsameline"
// ---- strings
f"truc-$m%10s-bidule" shouldBe "truc- machin-bidule"
f"truc-$m%-10s-bidule" shouldBe "truc-machin -bidule"
// ---- int, long
f"truc-$x%d-bidule" shouldBe "truc-42-bidule"
f"truc-$x%05d-bidule" shouldBe "truc-00042-bidule"
f"truc-$bx%,d-bidule" shouldBe "truc-42,042-bidule" // or space instead of comma in french locale
f"truc-$d%.1f-bidule" shouldBe "truc-0.4-bidule"
// ---- double,float
f"truc-$f%6.1f-bidule" shouldBe "truc- 42.4-bidule"
f"truc-$f%06.1f-bidule" shouldBe "truc-0042.4-bidule"
f"truc-$f%-6.1f-bidule" shouldBe "truc-42.4 -bidule"
f"truc-$bf%#,8.1f-bidule" shouldBe "truc- 2,442.4-bidule"
}
// ---------------------------------------------------------------------------------------------
"pattern matching with string interpolators" should "be possible since scala 2.13" in {
val name = "hello dave !" match { // of course to be "safe" add a case capture for all other cases
case s"hello $name !" => name
}
name shouldBe "dave"
}
it should "also be possible with this alternative syntax" in {
val s"hello $name !" = "hello dave !" // of course it is not "safe"
name shouldBe "dave"
}
"numbers" should "be extracted from strings with Locale taken into account" in {
val nf = NumberFormat.getInstance(Locale.FRENCH)
nf.parse("123,42").doubleValue() shouldBe 123.42
nf.parse("32123,42").doubleValue() shouldBe 32123.42
}
it should "be possible to take into account custom thousands separator (US)" in {
val nf = NumberFormat.getInstance(Locale.US).asInstanceOf[DecimalFormat]
val symbols = nf.getDecimalFormatSymbols
symbols.setGroupingSeparator(' ')
nf.setDecimalFormatSymbols(symbols)
nf.setGroupingUsed(true)
nf.parse("-32 123.42").doubleValue() shouldBe -32123.42
}
it should "be possible to take into account custom thousands separator (FR)" in {
val nf = NumberFormat.getInstance(Locale.FRENCH).asInstanceOf[DecimalFormat]
val symbols = nf.getDecimalFormatSymbols
symbols.setGroupingSeparator(' ')
nf.setDecimalFormatSymbols(symbols)
nf.setGroupingUsed(true)
nf.parse("-32 123,42").doubleValue() shouldBe -32123.42
}
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[AdvancedStringOperations].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment