Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Concatenating strings


Concatenation can be done in a variety of ways in Dart (refer to the concat_trim_strings file, and download it from www.packtpub.com/support).

How to do it...

Strings can be concatenated as follows:

  String s1 = "Dart", s2 = "Cook", s3 = "Book";
  var res = "Dart" " Cook" "Book";        (1)
  res = "Dart"  " Cook"
              "Book";        (2)
  res = s1 + " " + s2 + s3;        (3)
  res = "$s1 $s2$s3";        (4)
  res = [s1, " ", s2, s3].join();        (5)

  var sb = new StringBuffer();        (6)
  sb.writeAll([s1, " ", s2, s3]);
  res = sb.toString();
  print(res); // Dart CookBook

How it works...

Adjacent string literals are taken together as one string as shown in line (1), even if they are on different lines as shown in line (2). The + operator does the same thing (3), as well as string interpolation (4), which is the preferred way. Still there is another way to add join() to List<String> as shown in line (5). The most efficient way, especially if you want...