| OLD | NEW |
| 1 <p> | 1 <p> |
| 2 A Dart string is a sequence of Unicode character codes. | 2 A Dart string is a sequence of Unicode character codes. |
| 3 You can use either single or double quotes | 3 You can use either single or double quotes |
| 4 to create a string: | 4 to create a string: |
| 5 </p> | 5 </p> |
| 6 | 6 |
| 7 {% highlight dart %} | 7 {% highlight dart %} |
| 8 var s1 = 'Single quotes work well for string literals.'; | 8 var s1 = 'Single quotes work well for string literals.'; |
| 9 var s2 = "Double quotes work just as well."; | 9 var s2 = "Double quotes work just as well."; |
| 10 var s3 = 'It\'s easy to escape the string delimiter.'; | 10 var s3 = 'It\'s easy to escape the string delimiter.'; |
| 11 var s4 = "It's even easier to just use the other string delimiter."; | 11 var s4 = "It's even easier to just use the other string delimiter."; |
| 12 {% endhighlight %} | 12 {% endhighlight %} |
| 13 | 13 |
| 14 <p> | 14 <p> |
| 15 You can put the value of an expression inside a string | 15 You can put the value of an expression inside a string |
| 16 by using ${<em>expression</em>}. | 16 by using ${<em>expression</em>}. |
| 17 If the expression is a variable, | 17 If the expression is a variable, |
| 18 you can skip the {}. | 18 you can skip the {}. |
| 19 </p> | 19 </p> |
| 20 | 20 |
| 21 {% highlight dart %} | 21 {% highlight dart %} |
| 22 var s = 'string interpolation'; | 22 var s = 'string interpolation'; |
| 23 | 23 |
| 24 assert('Dart has $s, which is very handy.' == | 24 assert('Dart has $s, which is very handy.' == |
| 25 'Dart has string interpolation, which is very handy.'); | 25 'Dart has string interpolation, which is very handy.'); |
| 26 assert('That deserves all caps. ${s.toUpperCase()} is very handy!' == | 26 assert('That deserves all caps. ${s.toUpperCase()} is very handy!' == |
| 27 'That deserves all caps. STRING INTERPOLATION is very handy!'); | 27 'That deserves all caps. STRING INTERPOLATION is very handy!'); |
| 28 {% endhighlight %} | 28 {% endhighlight %} |
| 29 | 29 |
| 30 <aside class="note"> | 30 <aside> |
| 31 <b>Note:</b> | 31 <div class="alert alert-info"> |
| 32 The <b>==</b> operator tests whether | 32 <strong>Tip:</strong> |
| 33 two objects are equivalent. | 33 The <b>==</b> operator tests whether |
| 34 In the case of strings, | 34 two objects are equivalent. |
| 35 they're equivalent | 35 In the case of strings, |
| 36 if they have the same characters. | 36 they're equivalent |
| 37 if they have the same characters. |
| 38 </div> |
| 37 </aside> | 39 </aside> |
| 38 | 40 |
| 39 <p> | 41 <p> |
| 40 You can concatenate strings using | 42 You can concatenate strings using |
| 41 adjacent string literals: | 43 adjacent string literals: |
| 42 </p> | 44 </p> |
| 43 | 45 |
| 44 {% highlight dart %} | 46 {% highlight dart %} |
| 45 var s = 'String ''concatenation' | 47 var s = 'String ''concatenation' |
| 46 " works even over line breaks."; | 48 " works even over line breaks."; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 {% highlight dart %} | 124 {% highlight dart %} |
| 123 var greetingTemplate = 'Hello, NAME!'; | 125 var greetingTemplate = 'Hello, NAME!'; |
| 124 var greeting = greetingTemplate.replaceAll(new RegExp("NAME"), 'Bob'); | 126 var greeting = greetingTemplate.replaceAll(new RegExp("NAME"), 'Bob'); |
| 125 assert(greeting != greetingTemplate); // greetingTemplate didn't change. | 127 assert(greeting != greetingTemplate); // greetingTemplate didn't change. |
| 126 {% endhighlight %} | 128 {% endhighlight %} |
| 127 | 129 |
| 128 Learn more about | 130 Learn more about |
| 129 <a href="/docs/library-tour/#strings-and-regular-expressions">string methods</a> | 131 <a href="/docs/library-tour/#strings-and-regular-expressions">string methods</a> |
| 130 in the library tour. | 132 in the library tour. |
| 131 | 133 |
| 134 </section> |
| 135 |
| 132 <section id="strings-buffer"> | 136 <section id="strings-buffer"> |
| 133 <h4> StringBuffer methods </h4> | 137 <h4> StringBuffer methods </h4> |
| 134 | 138 |
| 135 <p> | 139 <p> |
| 136 To programmatically generate a string, you can use | 140 To programmatically generate a string, you can use |
| 137 <a href="http://api.dartlang.org/dart_core/StringBuffer.html">StringBuffer</a>. | 141 <a href="http://api.dartlang.org/dart_core/StringBuffer.html">StringBuffer</a>. |
| 138 A StringBuffer doesn't generate a new String object | 142 A StringBuffer doesn't generate a new String object |
| 139 until toString() is called. | 143 until toString() is called. |
| 140 </p> | 144 </p> |
| 141 | 145 |
| 142 {% highlight dart %} | 146 {% highlight dart %} |
| 143 var sb = new StringBuffer(); | 147 var sb = new StringBuffer(); |
| 144 | 148 |
| 145 sb.add("Use a StringBuffer "); | 149 sb.add("Use a StringBuffer "); |
| 146 sb.addAll(["for ", "efficient ", "string ", "creation "]); | 150 sb.addAll(["for ", "efficient ", "string ", "creation "]); |
| 147 sb.add("if you are ").add("building lots of strings."); | 151 sb.add("if you are ").add("building lots of strings."); |
| 148 | 152 |
| 149 var fullString = sb.toString(); | 153 var fullString = sb.toString(); |
| 150 | 154 |
| 151 assert(fullString == | 155 assert(fullString == |
| 152 'Use a StringBuffer for efficient string creation ' | 156 'Use a StringBuffer for efficient string creation ' |
| 153 'if you are building lots of strings.'); | 157 'if you are building lots of strings.'); |
| 154 | 158 |
| 155 sb.clear(); // All gone! | 159 sb.clear(); // All gone! |
| 156 assert(sb.toString() == ''); | 160 assert(sb.toString() == ''); |
| 157 {% endhighlight %} | 161 {% endhighlight %} |
| 158 | 162 |
| 159 <br> | 163 <aside> |
| 160 <aside class="note"> | 164 <div class="alert alert-info"> |
| 161 <b>Note:</b> | 165 <strong>Tip:</strong> |
| 162 StringBuffers are currently slow when compiled to JavaScript. | 166 StringBuffers are currently slow when compiled to JavaScript. |
| 163 See bug #<a href="http://code.google.com/p/dart/issues/detail?id=1216">1216</a> | 167 See bug #<a href="http://code.google.com/p/dart/issues/detail?id=1216">1216<
/a> |
| 164 for details. | 168 for details. |
| 169 </div> |
| 165 </aside> | 170 </aside> |
| 166 | 171 |
| 167 </section> | 172 </section> |
| OLD | NEW |