LoginSignup
1
0

More than 5 years have passed since last update.

Dartでmulti-line string(例:var s = '''サンプル''')

Last updated at Posted at 2018-06-07

Flutterのこちらのチュートリアルに沿って以下のコードを書いていて、ちょっと気になる箇所がありました。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //...
    Widget textSection = new Container(
      padding: const EdgeInsets.all(32.0),
      child: new Text(
        '''
Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese 
Alps.Situated 1,578 meters above sea level, it is one of the larger 
Alpine Lakes. A gondola ride from Kandersteg, followed by a half-hour 
walk through pastures and pine forest, leads you to the lake, which 
warms to 20 degrees Celsius in the summer. Activities enjoyed here 
include rowing, and riding the summer toboggan run.
        ''',
        softWrap: true,
      ),
    );
  //...
}

'''Lake Oeschinen...'''という文字列リテラルをnew Text()の引数に渡してあげていますが、このLake Oeschinen...という文字列がなぜ シングルクォート1つ(')あるいはダブルクォート('')ではなく、シングルクォート3つ(''')で囲われているのかなぁと疑問に思いました。マークダウンでもないし。

他言語でも使うのかもしれませんが、勉強不足で初めて見る感じ。
(ざっと調べた感じ、少なくとも他にScalaという言語では使うのでしょうか)

Dartのlanguage tourのStringの項目を見てみると、どうやらこれは('''も含めて)複数行にわたって文字列リテラルを指定するときに使うそうです。

Another way to create a multi-line string: use a triple quote with either single or double quotation marks:

↑でいうtriple quote'''です。

コード例は以下。

var s1 = '''
You can create
multi-line strings like this one.
''';

var s2 = """This is also a
multi-line string.""";

Javaみたいに+を使ったり、StringBuilderを使わなくても良くて楽ですね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0