Kotlin多行字符串中转义的模板 [英] Templates escaping in Kotlin multiline strings

查看:669
本文介绍了Kotlin多行字符串中转义的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在多行字符串中使用$登录,该如何转义?

If I want to use $ sign in multiline strings, how do I escape it?

val condition = """ ... $eq ... """

$eq被解析为对变量的引用.如何转义$,这样它就不会被识别为对变量的引用? (科特林M13)

$eq is parsed as a reference to a variable. How to escape $, so that it will not be recognized as reference to variable? (Kotlin M13)

推荐答案

来自文档

原始字符串由三引号(")分隔,不包含 转义,并且可以包含换行符和任何其他字符

A raw string is delimited by a triple quote ("""), contains no escaping and can contain newlines and any other character

您需要在换行符中使用标准字符串

You would need to use a standard string with newlines

" ...\n \$eq \n ... "

或者您可以使用文字表示形式

or you could use the literal representation

""" ... ${'$'}eq ... "

s: http://kotlinlang.org/docs/reference/basic-types.html #string-literals

这篇关于Kotlin多行字符串中转义的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆