如何在Kotlin中进行换行(换行) [英] How can I do a line break (line continuation) in Kotlin

查看:2589
本文介绍了如何在Kotlin中进行换行(换行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长行代码,我想在多行中分解.我使用什么,语法是什么?

I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?

例如,添加一串字符串:

For example, adding a bunch of strings:

val text = "This " + "is " + "a " + "long " + "long " + "line"

推荐答案

在Kotlin中没有用于行继续的符号.由于其语法在几乎所有符号之间都留有空格,因此您可以破坏语句:

There is no symbol for line continuation in Kotlin. As its grammar allows spaces between almost all symbols, you can just break the statement:

val text = "This " + "is " + "a " +
        "long " + "long " + "line"

但是,如果语句的第一行是有效的语句,则它将不起作用:

However, if the first line of the statement is a valid statement, it won't work:

val text = "This " + "is " + "a "
        + "long " + "long " + "line" // syntax error

为避免在多行中破坏长语句时出现此类问题,可以使用括号:

To avoid such issues when breaking long statements across multiple lines you can use parentheses:

val text = ("This " + "is " + "a "
        + "long " + "long " + "line") // no syntax error

有关详细信息,请参见科林语法.

For more information, see Kotlin Grammar.

这篇关于如何在Kotlin中进行换行(换行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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