val s = """ """Shom """
gives
:1: error: ';' expected but string literal found.
val s = """ """Shom"""
Tried to escape
val s = """ ""\"Shom """
result is not as expected.
s: String = " ""\"Shom"
Try with s string interpolator
val tripleQuote = """"""""" // 9 quotes in total
s"""${tripleQuote}Shom"""
res2: String = """Shom
or even inline it
s"""${"""""""""}Shom"""
s"""${List.fill(3)('"').mkString}Shom"""
s"""${"\""*3}Shom"""
which all output
"""Shom
because s String interpolators can take arbitrary expressions
s"meaning is ${41 + 1}"
// res4: String = meaning is 42
Related
In Kotlin, i need to find the string value after and before of specific character,
For example,
Item1. myResultValue: someother string
So in the above, I need to get the value after ". " and before ":" SO my output should be "myResultValue". If it doesn't match it can return empty or null value.
Please help to write to Regex for this.
For your specific case you can use regex:
val text = "Item1. myResultValue: someother string"
val pattern = Pattern.compile(".*\\.(.*):.*")
val matcher = pattern.matcher(text)
if (matcher.matches()) {
println(matcher.group(1))
}
If you want to build something smarter that defines the start and end pattern you can do it like this:
val startSearch = "\\."
val endSearch = ":"
val text = "Item1. myResultValue: someother string"
val pattern = Pattern.compile(".*$startSearch(.*)$endSearch.*")
val matcher = pattern.matcher(text)
if (matcher.matches()) {
println(matcher.group(1))
}
How can I write a string that spans multiple lines?
I tried to use the line continuation character \ at the end like many other languages, but it does not seem to work. The following:
val s = "line1\
line2";
generates an error:
/tmp/...:1: error: unexpected character l in \ ... \
/tmp/...:1: error: no matching quote found on this line
Not using any line continuation as follows generates a similar error:
val s = "line1
line2";
What's the correct way to write a multiline string?
You have to put the backslash at the beginning of the next lines as well :
print "this is\
\ a string on\
\ 3 lines\n";
If you're using SML/NJ and you don't care much about portability, there's a lesser known feature that could give you not only multiline strings, but also variable interpolation. SML/NJ supports an extensible quotation mechanism, which is described in more detail here: https://www.smlnj.org/doc/quote.html
Now, to solve your problem, we could define the following quotation parser. I'm calling it s to draw a parallel with Scala's s"..." syntax, which will become clearer in a few moments.
val s : string SMLofNJ.frag list -> string =
let
fun fold (SMLofNJ.QUOTE s, acc) = acc ^ s
| fold (SMLofNJ.ANTIQUOTE s, acc) = acc ^ s
in
List.foldl fold ""
end
Notice that it makes use of the SMLofNJ structure to access compiler-specific datatypes and values (frag, QUOTE and ANTIQUOTE).
We can now use the above like this (make sure you're using the command line option -Cparser.quotations=true):
$ sml -Cparser.quotations=true
- val a = "var";
val a = "var" : string
-
- val b = `this is a raw quotation using ^a`;
val b = [QUOTE "this is a raw quotation using ",ANTIQUOTE "var",QUOTE ""] :
string SMLofNJ.frag list
-
- val c = s`this is an interpolated string: ^a`;
val c = "this is an interpolated string: var" : string
-
- print c;
this is an interpolated string: varval it = () : unit
-
- val d = s`this
… is
… a
… multiline string: ^a`;
val d = "this\nis\na\nmultiline string: var" : string
-
- print d;
this
is
a
multiline string: varval it = () : unit
As you can see, using quotations in conjunction with a very simple custom parser (the s function), we can get multiline interpolated strings in SML/NJ.
In my personal library, I've defined it in a bit more structured way:
structure Strings =
struct
structure Interpolated =
struct
local
open SMLofNJ
in
(**
* Support for multiline, interpolated strings.
*
* NB: requires SML/NJ quotations: sml -Cparser.quotations=true.
*
* ```sml
* - open Strings.Interpolated;
* - val v = "val";
* val v = "val" : string
* - val j = s`{
* = "key": "^v"
* = }`;
* val j = "{\n \"key\": \"val\"\n}" : string
* ```
*)
val s : string frag list -> string =
let
fun fold (QUOTE s, acc) = acc ^ s
| fold (ANTIQUOTE s, acc) = acc ^ s
in
List.foldl fold ""
end
end
end
end
Let's say I have this string:
val mainString: String = "AAA.BBB"
And now I define two children strings:
val firstString: String = ""
val secondString: String = ""
What code should I write to make firstString equals to "AAA", and secondString equals to "BBB"?
The below code works for any amount of strings separated by delimiters
val texto = "111.222.333"
val vet = texto.split(".")
for (st in vet) println(st)
It prints
111
222
333
we are trying to add single quotes to each element of a set of string's in Scala
val s : Set[String] = Set("abcd","cdef")
Expected outcome is val s : Set[String] = Set("'abcd'","'cdef'")
tried multiple approaches
Approach1:
s.map(x => x.mkString("'", "', '", "'"))
Approach2:
s.map(x => '\'' + x + '\'')
Fairly new to Scala if someone could please point us in the right direction it would be of great help
I don't think there is no big difference between Scala and whichever language you've been using.
One thing you might want to use is String interpolation
and then you can write
s.map(x => s"'$x'")
you cannot similar similar pattern to wrap it with double quotes. So, when it comes to single or double quotes wrapping, go for hex values. hex 22 is for double quotes and hex 27 for single quotes. It will be easy to remember.
scala> val sq = Integer.parseInt("27",16).toChar
sq: Char = '
scala> val dq = Integer.parseInt("22",16).toChar
dq: Char = "
scala> val s : Set[String] = Set("abcd","cdef")
s: Set[String] = Set(abcd, cdef)
scala> s.map(x => s"$sq$x$sq")
res14: scala.collection.immutable.Set[String] = Set('abcd', 'cdef')
scala> s.map(x => s"$dq$x$dq")
res15: scala.collection.immutable.Set[String] = Set("abcd", "cdef")
hope, this adds clarity to such scenarios.
When I split a String using , works as expected :
val line1 = "this,is,a,test" //> line1 : String = this,is,a,test
val sLine = line1.split(",")
however if I use | the String is split into its character elements and added to array :
val line1 = "this|is|a|test" //> line1 : String = this|is|a|test
val sLine = line1.split("|") //> sLine : Array[String] = Array("", t, h, i, s, |, i, s, |, a, |, t, e, s, t)
Why is this occurring because of | character ?
possible solutions
val sLine2 = line1.split('|')
because ' denotes a character, a single character, split does not treat it as a regexp
val sLine2 = line1.split("\\|")
to escape the special alternation | regexp character. This is why it isn't working. split is treating | as a zero width regexp and so the string is vapourized into its component characters
As pipe is a special regex character, I believe you need to escape it like so "\\|" in order for it to work
scala> val line1 = "this,is,a,test"
line1: java.lang.String = this,is,a,test
scala> line1.split(",")
res2: Array[java.lang.String] = Array(this, is, a, test)
scala> var line2 = "this|is|a|test"
line2: java.lang.String = this|is|a|test
scala> line2.split("\\|")
res3: Array[java.lang.String] = Array(this, is, a, test)