Last active
May 25, 2024 10:20
-
-
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/87c86776484cfb70c513cb1ad4b4cad8ab5cd59a
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
// 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.4.2" | |
//> 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