サーチ…


連結と補間

プラス( + )演算子を使用して文字列を連結することができます。

'Dart ' + 'is ' + 'fun!'; // 'Dart is fun!'

隣接する文字列リテラルを連結に使用することもできます。

'Dart ' 'is ' 'fun!';    // 'Dart is fun!'

${}を使用して、文字列内のDart式の値を補間することができます。中括弧は、識別子を評価するときに省略することができます。

var text = 'dartlang';
'$text has ${text.length} letters'; // 'dartlang has 8 letters'

有効な文字列

文字列は、単一行または複数行にできます。単一行文字列は一重引用符または二重引用符を使用して記述され、複数行文字列は三重引用符を使用して記述されます。以下はすべて有効なDart文字列です:

'Single quotes';
"Double quotes";
'Double quotes in "single" quotes';
"Single quotes in 'double' quotes";

'''A
multiline
string''';

"""
Another
multiline
string""";

パーツからのビルド

プログラムでストリングを生成するには、 StringBufferを使用するのが最適です。 StringBufferは、 toString()が呼び出されるまで、新しいStringオブジェクトを生成しません。

var sb = new StringBuffer();

sb.write("Use a StringBuffer");
sb.writeAll(["for ", "efficient ", "string ", "creation "]);
sb.write("if you are ")
sb.write("building lots of strings");

// or you can use method cascades:

sb
  ..write("Use a StringBuffer")
  ..writeAll(["for ", "efficient ", "string ", "creation "])
  ..write("if you are ")
  ..write("building lots of strings");

var fullString = sb.toString();

print(fullString); 
// Use a StringBufferfor efficient string creation if you are building lots of strings

sb.clear(); // all gone!


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow