https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

Stylianos Gakis

02/24/2022, 9:20 AM
I was looking to use the
String.replace
function to replace some text. Specifically there was a place where this shows:
Copy code
"""
  """
And I’d just like to replace it with:
Copy code
"""
I was looking to use a triple quote String as I really like how I don’t need to escape stuff in there because it confuses me, but then I end up in this situation where I can’t really match a “”" inside it as it closes the string 😅 Is my only solution here to not use triple quotes since I can’t escape a “”" in there probably right? With a “\”\“\”\n \“\”\“” string instead?
s

Sam

02/24/2022, 9:28 AM
A simple compromise would be to define a constant containing the `"""`:
Copy code
const val qqq = "\"\"\""
val x = """$qqq
  $qqq"""
println(x)
I'm sure there are better solutions, that's just something that came to mind
e

ephemient

02/24/2022, 11:26 AM
you can avoid the inner
"""
with
Copy code
val x = """
""${'"'}
  ""${'"'}
                """
as well, but in this case I'd find Sam's solution or
Copy code
val x = """
qqq
  qqq
                """.replace('q', '"')
to be cleaner
s

Stylianos Gakis

02/24/2022, 11:37 AM
Very interesting workarounds, both of them! Such a weird problem to stumble upon, but I guess properly parsing this would just be impossible with how I initially wanted it to look like.