Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: sdk/lib/core/string.dart

Issue 23480035: Added examples to String docs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * A class for working with a sequence of characters. 8 * A class for working with a sequence of characters.
mem 2013/09/05 23:09:09 how about just "A sequence of characters." One of
shailentuli 2013/09/23 11:50:27 Punting to kathy.
Kathy Walrath 2013/09/25 18:47:38 What mem sed.
9 * 9 *
10 * A string can be either single or multiline. Single line strings are
11 * written using matching single or double quotes, and mutliline strings are
12 * written using triple quotes. These are all valid Dart strings:
mem 2013/09/05 23:09:09 The following are all ...
shailentuli 2013/09/23 11:50:27 Done.
13 *
14 * 'Single quotes';
15 * "Double quotes";
16 * 'Double quotes in "single" quotes';
17 * "Single quotes in 'double' quotes";
18 *
19 * '''A
20 * multiline
21 * string''';
22 *
23 * """
24 * Another
25 * multiline
26 * string""";
27 *
28 * Strings are immutable. While you cannot change a string, you can perform an
mem 2013/09/05 23:09:09 Although you cannot...
shailentuli 2013/09/23 11:50:27 Done.
29 * operation on a string and assign the result to a new string:
30 *
31 * var string = 'Dart is fun';
32 * var newString = string.toUpperCase();
mem 2013/09/05 23:09:09 Do you need // 'Dart is fun' // 'DART IS FUN' ??
shailentuli 2013/09/23 11:50:27 Not really. I'm just showing assignment.
33 *
34 * You can use the `+` operator to concatenate strings:
mem 2013/09/05 23:09:09 You can use the plus (`+`) operator ...
shailentuli 2013/09/23 11:50:27 Done.
35 *
36 * 'Dart ' + 'is ' + 'fun!'; // 'Dart is fun!'
37 *
38 * You can also use string literals for concatenation:
mem 2013/09/05 23:09:09 You can also use adjacent string literals ...
shailentuli 2013/09/23 11:50:27 Done.
39 *
40 * 'Dart ' 'is ' 'fun!'; // 'Dart is fun!'
41 *
42 * You can use `${}` syntax for interpolating the value of Dart expressions
43 * within strings. The curly braces can be omitted when evaluating identifiers:
mem 2013/09/05 23:09:09 You can use `${}` ... also for interpolating -> t
shailentuli 2013/09/23 11:50:27 Done.
44 *
45 * string = 'dartlang';
46 * '$string has ${string.length} letters'; // 'dartlang has 8 letters'
47 *
10 * A string is represented by a sequence of Unicode UTF-16 code units 48 * A string is represented by a sequence of Unicode UTF-16 code units
11 * accessible through the [codeUnitAt] or the [codeUnits] members. Their 49 * accessible through the [codeUnitAt] or the [codeUnits] members:
mem 2013/09/05 23:09:09 --> accessible with [codeUnits] or [codeUnitAt]:
shailentuli 2013/09/23 11:50:27 Leaving Florian's ordering. Re-ordered my example.
12 * string representation is accessible through the index-operator. 50 *
51 * string = 'Dart';
52 * string.codeUnits; // [68, 97, 114, 116]
53 * string.codeUnitAt(0); // 68
54
55 * The string representation of the code units is accessible through the index
mem 2013/09/05 23:09:09 The string representation -> the character represe
shailentuli 2013/09/23 11:50:27 No, better as string. String is well understood, c
56 * operator:
57 *
58 * string[0]; // 'D'
13 * 59 *
14 * The characters of a string are encoded in UTF-16. Decoding UTF-16, which 60 * The characters of a string are encoded in UTF-16. Decoding UTF-16, which
15 * combines surrogate pairs, yields Unicode code points. Following a similar 61 * combines surrogate pairs, yields Unicode code points. Following a similar
16 * terminology to Go we use the name "rune" for an integer representing a 62 * terminology to Go we use the name 'rune' for an integer representing a
mem 2013/09/05 23:09:09 Remove reference to Go....also reword... A 'rune'
shailentuli 2013/09/23 11:50:27 Keeping Florian's language. He wants the Go refere
17 * Unicode code point. The runes of a string are accessible through the [runes] 63 * Unicode code point. The runes of a string are accessible through the [runes]
18 * getter. 64 * property:
mem 2013/09/05 23:09:09 Use the [runes] property to get the runes of a str
19 * 65 *
20 * Strings are immutable. 66 * string.runes.toList(); // [68, 97, 114, 116]
67 *
68 * For a character outside the Basic Multilingual Plane (plane 0) that is
mem 2013/09/05 23:09:09 I feel like this para is too much for the class-le
mem 2013/09/05 23:09:09 you haven't defined the basic multilingual plane
shailentuli 2013/09/23 11:50:27 No, keeping it. This runes vs. codeUnits thing app
shailentuli 2013/09/23 11:50:27 That's OK. This is not a Dart concept. Its a big t
69 * composed of a surrogate pair, [runes] combines the pair and returns a
70 * single integer. For example, the Unicode character for a
71 * musical G-clef ('𝄞') with rune value 0x1D11E consists of a UTF-16 surrogate
72 * pair: `0xD834` and `0xDD1E`. Using `codeUnits` returns the surrogate pair,
73 * and using `runes` returns their combined value:
mem 2013/09/05 23:09:09 Remove 'using', link to codeUnits --> [codeUnits]
shailentuli 2013/09/23 11:50:27 Keeping it as is, because I don't want the sentenc
74 *
75 * var clef = '\u{1D11E}';
76 * clef.codeUnits; // [0xD834, 0xDD1E]
77 * clef.runes.toList(); // [0x1D11E]
21 * 78 *
22 * It is a compile-time error for a class to attempt to extend or implement 79 * It is a compile-time error for a class to attempt to extend or implement
23 * String. 80 * String.
mem 2013/09/05 23:09:09 Extending or implementing String is a compile-time
shailentuli 2013/09/23 11:50:27 Done.
24 * 81 *
25 * For concatenating strings efficiently, use the [StringBuffer] class. For 82 * ## Other resources
26 * working with regular expressions, use the [RegExp] class. 83 *
84 * See [StringBuffer] to efficiently build a string incrementally. See
mem 2013/09/05 23:09:09 delete 'incrementally'
shailentuli 2013/09/23 11:50:27 No, keeping it. 'Build a string' by itself reads p
85 * [RegExp] to work with regular expressions.
86 *
87 * See the [Dart Cookbook](https://www.dartlang.org/docs/cookbook/#strings)
88 * for String examples and recipes.
mem 2013/09/05 23:09:09 Link to Dart Up & Running, language tour, and libr
shailentuli 2013/09/23 11:50:27 Linking to the Library tour.
89 *
27 */ 90 */
28 abstract class String implements Comparable<String>, Pattern { 91 abstract class String implements Comparable<String>, Pattern {
29 /** 92 /**
30 * Allocates a new String for the specified [charCodes]. 93 * Allocates a new String for the specified [charCodes].
31 * 94 *
32 * The [charCodes] can be UTF-16 code units or runes. If a char-code value is 95 * The [charCodes] can be UTF-16 code units or runes. If a char-code value is
33 * 16-bit it is copied verbatim. If it is greater than 16 bits it is 96 * 16-bit, it is copied verbatim:
34 * decomposed into a surrogate pair. 97 *
98 * new String.fromCharCodes([68]); // 'D'
99 *
100 * If it is greater than 16-bits, it is decomposed into a surrogate pair:
mem 2013/09/05 23:09:09 If a char-code value is greater than 16-bits,
shailentuli 2013/09/23 11:50:27 Done.
101 *
102 * var clef = new String.fromCharCodes([0x1D11E]);
103 * clef.codeUnitAt(0); // 0xD834
104 * clef.codeUnitAt(1); // 0xDD1E
35 */ 105 */
36 external factory String.fromCharCodes(Iterable<int> charCodes); 106 external factory String.fromCharCodes(Iterable<int> charCodes);
37 107
38 /** 108 /**
39 * Allocates a new String for the specified [charCode]. 109 * Allocates a new String for the specified [charCode].
40 * 110 *
41 * The new string contains a single code unit if the [charCode] can be 111 * The new string contains a single code unit if the [charCode] can be
42 * represented by a single UTF-16 code unit. Otherwise the [length] is 2 and 112 * represented by a single UTF-16 code unit. Otherwise the [length] is 2 and
mem 2013/09/05 23:09:09 Reverse the sense of the first sentence. Add comma
shailentuli 2013/09/23 11:50:27 Done.
43 * the code units form a surrogate pair. 113 * the code units form a surrogate pair. See documentation for
114 * [fromCharCodes].
44 * 115 *
45 * It is allowed (though generally discouraged) to create a String with only 116 * It is allowed (though generally discouraged) to create a String with only
46 * one half of a surrogate pair. 117 * one half of a surrogate pair.
mem 2013/09/05 23:09:09 It is allowed is passive. How about the following
shailentuli 2013/09/23 11:50:27 Done.
47 */ 118 */
48 factory String.fromCharCode(int charCode) { 119 factory String.fromCharCode(int charCode) {
49 List<int> charCodes = new List<int>.filled(1, charCode); 120 List<int> charCodes = new List<int>.filled(1, charCode);
50 return new String.fromCharCodes(charCodes); 121 return new String.fromCharCodes(charCodes);
51 } 122 }
52 123
53 /** 124 /**
54 * Gets the character (as a single-code-unit [String]) at the given [index]. 125 * Gets the character (as a single-code-unit [String]) at the given [index].
55 * 126 *
56 * The returned string represents exactly one UTF-16 code unit which may be 127 * The returned string represents exactly one UTF-16 code unit, which may be
57 * half of a surrogate pair. For example the Unicode character for a 128 * half of a surrogate pair. A single member of a surrogate pair is an
58 * musical G-clef ("𝄞") with rune value 0x1D11E consists of a UTF-16 surrogate 129 * invalid UTF-16 string:
59 * pair: `0xD834` and `0xDD1E`. Using the index-operator on this string yields
60 * a String with half of a surrogate pair:
61 * 130 *
62 * var clef = "\u{1D11E}"; 131 * var clef = "\u{1D11E}";
63 * clef.length; // => 2 132 * // These represent invalid UTF-16 strings.
64 * clef.runes.first == 0x1D11E; // => true 133 * clef[0].codeUnits; // [0xD834]
65 * clef.runes.length; // => 1 134 * clef[1].codeUnits; // [0xDD1E]
66 * clef.codeUnitAt(0); // => 0xD834
67 * clef.codeUnitAt(1); // => 0xDD1E
68 * // The following strings are halves of a UTF-16 surrogate pair and
69 * // thus invalid UTF-16 strings:
70 * clef[0]; // => a string of length 1 with code-unit value 0xD834.
71 * clef[1]; // => a string of length 1 with code-unit value 0xDD1E.
72 * 135 *
73 * This method is equivalent to 136 * This method is equivalent to
74 * `new String.fromCharCode(this.codeUnitAt(index))`. 137 * `new String.fromCharCode(this.codeUnitAt(index))`.
75 */ 138 */
76 String operator [](int index); 139 String operator [](int index);
77 140
78 /** 141 /**
79 * Returns the 16-bit UTF-16 code unit at the given [index]. 142 * Returns the 16-bit UTF-16 code unit at the given [index].
80 */ 143 */
81 int codeUnitAt(int index); 144 int codeUnitAt(int index);
82 145
83 /** 146 /**
84 * The length of the string. 147 * The length of the string.
85 * 148 *
86 * Returns the number of UTF-16 code units in this string. The number 149 * Returns the number of UTF-16 code units in this string. The number
87 * of [runes] might be less, if the string contains characters outside 150 * of [runes] might be less, if the string contains characters outside
mem 2013/09/05 23:09:09 might be less -> might be fewer
shailentuli 2013/09/23 11:50:27 Done.
88 * the basic multilingual plane (plane 0). 151 * the Basic Multilingual Plane (plane 0). For example:
152 *
153 * 'Dart'.length; // 4
154 * 'Dart'.runes.length; // 4
155 *
156 * var clef = "\u{1D11E}";
157 * clef.length; // 2
158 * clef.runes.length; // 1
89 */ 159 */
90 int get length; 160 int get length;
91 161
92 /** 162 /**
93 * Returns whether the two strings are equal. 163 * Returns whether the two strings are equal.
mem 2013/09/05 23:09:09 Returns true if the two strings are equal. False,
94 * 164 *
95 * This method compares each individual code unit of the strings. 165 * This method compares each individual code unit of the strings.
96 * Equivalently (for strings that are well-formed UTF-16) it compares each 166 * Equivalently (for strings that are well-formed UTF-16) it compares each
mem 2013/09/05 23:09:09 For strings that are well-formed UTF-16, it compar
shailentuli 2013/09/23 11:50:27 Done.
97 * individual rune (code point). It does not check for Unicode equivalence. 167 * individual rune (code point). It does not check for Unicode equivalence.
98 * For example the two following strings both represent the string "Amélie" 168 * For example, both the following strings represent the string "Amélie",
99 * but, due to their different encoding will not return equal. 169 * but due to their different encoding, are not equal:
100 * 170 *
101 * "Am\xe9lie" 171 * 'Am\xe9lie' == 'Ame\u{301}lie'; // false
102 * "Ame\u{301}lie"
103 * 172 *
104 * In the first string the "é" is encoded as a single unicode code unit (also 173 * In the first string the "é" is encoded as a single unicode code unit (also
mem 2013/09/05 23:09:09 The first string encodes "é" as a single unicode c
shailentuli 2013/09/23 11:50:27 Done.
105 * a single rune), whereas the second string encodes it as "e" with the 174 * a single rune), whereas the second string encodes it as "e" with the
mem 2013/09/05 23:09:09 The second string encodes "é" as the letter "e" an
shailentuli 2013/09/23 11:50:27 Done.
106 * combining accent character "◌́". 175 * combining accent character "◌́".
107 */ 176 */
108 bool operator ==(var other); 177 bool operator ==(var other);
109 178
110 /** 179 /**
111 * Returns whether this string ends with [other]. 180 * Returns whether this string ends with [other]. For example:
mem 2013/09/05 23:09:09 Return true if this string ...
shailentuli 2013/09/23 11:50:27 Done.
181 *
182 * 'Dart'.endsWith('t'); // true
112 */ 183 */
113 bool endsWith(String other); 184 bool endsWith(String other);
114 185
115 /** 186 /**
116 * Returns whether this string starts with a match of [pattern]. 187 * Returns whether this string starts with a match of [pattern].
mem 2013/09/05 23:09:09 Returns true if this string...
shailentuli 2013/09/23 11:50:27 Done.
117 * 188 *
118 * If [index] is provided, instead check if the substring starting 189 * var string = 'Dart';
119 * at that index starts with a match of [pattern]. 190 * string.startsWith('D'); // true
191 * string.startsWith(new RegExp(r'[A-Z][a-z]')); // true
192 *
193 * If [index] is provided, instead checks if the substring starting
mem 2013/09/05 23:09:09 instead -> this method
shailentuli 2013/09/23 11:50:27 Done.
194 * at that index starts with a match of [pattern]:
195 *
196 * string.startsWith('art', 1); // true
197 * string.startsWith(new RegExp(r'\w{3}')); // true
120 * 198 *
121 * It is an error if [index] is negative or greater than [length]. 199 * It is an error if [index] is negative or greater than [length].
mem 2013/09/05 23:09:09 An error occurs if ...
shailentuli 2013/09/23 11:50:27 Done.
122 * 200 *
123 * A [RegExp] containing "^" will not match if the [index] is greater than 201 * A [RegExp] containing '^' will not match if the [index] is greater than
mem 2013/09/05 23:09:09 will -> does
mem 2013/09/05 23:09:09 Here single quotes are used to delineate a charact
shailentuli 2013/09/23 11:50:27 Done.
shailentuli 2013/09/23 11:50:27 Done.
124 * zero. The pattern works on the string as a whole, and does not extract 202 * zero. The pattern works on the string as a whole, and does not extract
125 * a substring starting at [index] first. That is. 203 * a substring starting at [index] first. For example:
126 * "abc".startsWith(new RegExp("^.", 1)) == false 204 *
205 * string.startsWith(new RegExp(r'^art'), 1); // false
206 * string.startsWith(new RegExp(r'art'), 1); // true
127 */ 207 */
128 bool startsWith(Pattern pattern, [int index = 0]); 208 bool startsWith(Pattern pattern, [int index = 0]);
129 209
130 /** 210 /**
131 * Returns the first position of a match of [pattern] in this string, 211 * Returns the first position of a match of [pattern] in this string,
mem 2013/09/05 23:09:09 Returns the position of the first match of [patter
shailentuli 2013/09/23 11:50:27 Done.
132 * starting at [start] (inclusive). 212 * starting at [start] (inclusive). For example:
133 * 213 *
134 * Returns -1 if a match could not be found. 214 * var string = 'Dartisans';
215 * string.indexOf('art'); // 1
216 * string.indexOf(new RegExp(r'[A-Z][a-z]')); // 0
135 * 217 *
136 * It is an error if start is negative or greater than [length]. 218 * Returns -1 if a match is not found:
mem 2013/09/05 23:09:09 Returns -1 if no match is found.
shailentuli 2013/09/23 11:50:27 Done.
219 *
220 * string.indexOf(new RegExp(r'dart')); // -1
221 *
222 * It is an error if [start] is negative or greater than [length].
mem 2013/09/05 23:09:09 An error occurs if ...
shailentuli 2013/09/23 11:50:27 Done.
137 */ 223 */
138 int indexOf(Pattern pattern, [int start]); 224 int indexOf(Pattern pattern, [int start]);
139 225
140 /** 226 /**
141 * Returns the last position of a match [pattern] in this string, searching 227 * Returns the last position of a match [pattern] in this string, searching
mem 2013/09/05 23:09:09 Returns the position of the last match of [pattern
shailentuli 2013/09/23 11:50:27 Done.
142 * backward starting at [start] (inclusive). 228 * backward starting at [start] (inclusive).
143 * 229 *
230 * var string = 'Dartisans';
231 * string.lastIndexOf('a'); // 6
232 * string.lastIndexOf(new RegExp(r'a(r|n)')); // 6
233 *
144 * Returns -1 if [other] could not be found. 234 * Returns -1 if [other] could not be found.
145 * 235 *
236 * string.lastIndexOf(new RegExp(r'DART')); // -1
237 *
146 * It is an error if start is negative or greater than [length]. 238 * It is an error if start is negative or greater than [length].
mem 2013/09/05 23:09:09 An error occurs ...
shailentuli 2013/09/23 11:50:27 Done.
147 */ 239 */
148 int lastIndexOf(Pattern pattern, [int start]); 240 int lastIndexOf(Pattern pattern, [int start]);
149 241
150 /** 242 /**
151 * Returns whether this string is empty. 243 * Returns whether this string is empty.
mem 2013/09/05 23:09:09 Returns true if this string is empty.
152 */ 244 */
153 bool get isEmpty; 245 bool get isEmpty;
154 246
155 /** 247 /**
156 * Returns whether this string is not empty. 248 * Returns whether this string is not empty.
mem 2013/09/05 23:09:09 Returns true if this string is not empty.
157 */ 249 */
158 bool get isNotEmpty; 250 bool get isNotEmpty;
159 251
160 /** 252 /**
161 * Creates a new string by concatenating this string with [other]. 253 * Creates a new string by concatenating this string with [other].
162 * 254 *
255 * 'dart' + 'lang'; // 'dartlang'
256 *
163 * A sequence of strings can be concatenated by using [Iterable.join]: 257 * A sequence of strings can be concatenated by using [Iterable.join]:
mem 2013/09/05 23:09:09 Use [Iterable.join] to concatenate a sequence of s
shailentuli 2013/09/23 11:50:27 Done.
164 * 258 *
165 * var strings = ['foo', 'bar', 'geez']; 259 * var fruits = ['apple', 'banana', 'orange'];
166 * var concatenated = strings.join(); 260 * fruits.join(" : "); // 'apple : banana : orange'
167 */ 261 */
168 String operator +(String other); 262 String operator +(String other);
169 263
170 /** 264 /**
171 * Returns a substring of this string in the given range. 265 * Returns a substring of this string in the given range.
mem 2013/09/05 23:09:09 Returns the substring of this string that extends
shailentuli 2013/09/23 11:50:27 Done.
172 * [startIndex] is inclusive and [endIndex] is exclusive. 266 * [startIndex] is inclusive and [endIndex] is exclusive. For example:
267 *
268 * var string = 'dartlang';
269 * string.substring(1); // 'artlang'
270 * string.substring(1, 4); // 'art'
173 */ 271 */
174 String substring(int startIndex, [int endIndex]); 272 String substring(int startIndex, [int endIndex]);
175 273
176 /** 274 /**
177 * Removes leading and trailing whitespace from a string. 275 * Removes leading and trailing whitespace from a string.
178 * 276 *
179 * If the string contains leading or trailing whitespace a new string with no 277 * If the string contains leading or trailing whitespace a new string with no
mem 2013/09/05 23:09:09 whitespace --> whitespace,
shailentuli 2013/09/23 11:50:27 Done.
180 * leading and no trailing whitespace is returned. Otherwise, the string 278 * leading and no trailing whitespace is returned:
181 * itself is returned. 279 *
280 * '\tDart is fun\n'.trim(); // 'Dart is fun'
281 *
282 * Otherwise, the string itself is returned:
mem 2013/09/05 23:09:09 the string itself --> the original string
shailentuli 2013/09/23 11:50:27 Done.
283 *
284 * 'Dart'.trim(); // 'Dart'
182 * 285 *
183 * Whitespace is defined by the Unicode White_Space property (as defined in 286 * Whitespace is defined by the Unicode White_Space property (as defined in
184 * version 6.2 or later) and the BOM character, 0xFEFF. 287 * version 6.2 or later) and the BOM character, 0xFEFF.
185 * 288 *
186 * Here is the list of trimmed characters (following version 6.2): 289 * Here is the list of trimmed characters (following version 6.2):
187 * 290 *
188 * 0009..000D ; White_Space # Cc <control-0009>..<control-000D> 291 * 0009..000D ; White_Space # Cc <control-0009>..<control-000D>
189 * 0020 ; White_Space # Zs SPACE 292 * 0020 ; White_Space # Zs SPACE
190 * 0085 ; White_Space # Cc <control-0085> 293 * 0085 ; White_Space # Cc <control-0085>
191 * 00A0 ; White_Space # Zs NO-BREAK SPACE 294 * 00A0 ; White_Space # Zs NO-BREAK SPACE
192 * 1680 ; White_Space # Zs OGHAM SPACE MARK 295 * 1680 ; White_Space # Zs OGHAM SPACE MARK
193 * 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR 296 * 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR
194 * 2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE 297 * 2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE
195 * 2028 ; White_Space # Zl LINE SEPARATOR 298 * 2028 ; White_Space # Zl LINE SEPARATOR
196 * 2029 ; White_Space # Zp PARAGRAPH SEPARATOR 299 * 2029 ; White_Space # Zp PARAGRAPH SEPARATOR
197 * 202F ; White_Space # Zs NARROW NO-BREAK SPACE 300 * 202F ; White_Space # Zs NARROW NO-BREAK SPACE
198 * 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE 301 * 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE
199 * 3000 ; White_Space # Zs IDEOGRAPHIC SPACE 302 * 3000 ; White_Space # Zs IDEOGRAPHIC SPACE
200 * 303 *
201 * FEFF ; BOM ZERO WIDTH NO_BREAK SPACE 304 * FEFF ; BOM ZERO WIDTH NO_BREAK SPACE
202 */ 305 */
203 String trim(); 306 String trim();
204 307
205 /** 308 /**
206 * Returns whether this string contains a match of [other]. 309 * Returns whether this string contains a match of [other]:
mem 2013/09/05 23:09:09 Returns true if this string contains a match of [o
shailentuli 2013/09/23 11:50:27 Done.
310 *
311 * var string = 'Dart strings';
312 * string.contains('D'); // true
313 * string.contains(new RegExp(r'[A-Z]')); // true
207 * 314 *
208 * If [startIndex] is provided, only matches at or after that index 315 * If [startIndex] is provided, only matches at or after that index
mem 2013/09/05 23:09:09 ..., this method only considers matches at or afte
shailentuli 2013/09/23 11:50:27 Done.
shailentuli 2013/09/23 11:50:27 Done.
209 * are considered. 316 * are considered:
317 *
318 * string.contains('X', 1); // false
319 * string.contains(new RegExp(r'[A-Z]'), 1); // false
210 * 320 *
211 * It is an error if [startIndex] is negative or greater than [length]. 321 * It is an error if [startIndex] is negative or greater than [length].
mem 2013/09/05 23:09:09 An error occurs if ...
shailentuli 2013/09/23 11:50:27 Done.
212 */ 322 */
213 bool contains(Pattern other, [int startIndex = 0]); 323 bool contains(Pattern other, [int startIndex = 0]);
214 324
215 /** 325 /**
216 * Returns a new string where the first occurence of [from] in this string 326 * Returns a new string where the first occurence of [from] in this string
mem 2013/09/05 23:09:09 where -> in which
shailentuli 2013/09/23 11:50:27 Done.
217 * is replaced with [to]. 327 * is replaced with [to]. For example:
328 *
329 * '0.0001'.replaceFirst(new RegExp(r'0+'), ''); // '.0001'
218 */ 330 */
219 String replaceFirst(Pattern from, String to); 331 String replaceFirst(Pattern from, String to);
220 332
221 /** 333 /**
222 * Replaces all substrings matching [from] with [replace]. 334 * Replaces all substrings matching [from] with [replace].
mem 2013/09/05 23:09:09 matching -> that match
223 * 335 *
224 * Returns a new string where the non-overlapping substrings that match 336 * Returns a new string where the non-overlapping substrings that match
mem 2013/09/05 23:09:09 where -> in which
225 * [from] (the ones iterated by `from.allMatches(thisString)`) are replaced 337 * [from] (the ones iterated by `from.allMatches(thisString)`) are replaced
226 * by the literal string [replace]. 338 * by the literal string [replace].
227 * 339 *
340 * 'resume'.replaceAll(new RegExp(r'e'), '\u00E9'); // 'résumé'
341 *
228 * Notice that the [replace] string is not interpreted. If the replacement 342 * Notice that the [replace] string is not interpreted. If the replacement
229 * depends on the match (for example on a [RegExp]'s capture groups), use 343 * depends on the match (for example on a [RegExp]'s capture groups), use
230 * the [replaceAllMapped] method instead. 344 * the [replaceAllMapped] method instead.
231 */ 345 */
232 String replaceAll(Pattern from, String replace); 346 String replaceAll(Pattern from, String replace);
233 347
234 /** 348 /**
235 * Replace all substrings matching [from] by a string computed from the match. 349 * Replace all substrings matching [from] by a string computed from the match.
mem 2013/09/05 23:09:09 matching -> that match
236 * 350 *
237 * Returns a new string where the non-overlapping substrings that match 351 * Returns a new string where the non-overlapping substrings that match
238 * [from] (the ones iterated by `from.allMatches(thisString)`) are replaced 352 * [from] (the ones iterated by `from.allMatches(thisString)`) are replaced
239 * by the result of calling [replace] on the corresponding [Match] object. 353 * by the result of calling [replace] on the corresponding [Match] object.
240 * 354 *
241 * This can be used to replace matches with new content that depends on the 355 * This can be used to replace matches with new content that depends on the
242 * match, unlike [replaceAll] where the replacement string is always the same. 356 * match, unlike [replaceAll] where the replacement string is always the same.
243 * 357 *
244 * Example (simplified pig latin): 358 * The [replace] function is called with the [Match] generated
359 * by the pattern, and its result is used as replacement.
360 *
361 * The function defined below converts each word in some text to 'pig latin'
mem 2013/09/05 23:09:09 some text -> a string
shailentuli 2013/09/23 11:50:27 Done.
362 * using [replaceAllMapped]:
363 *
245 * pigLatin(String words) => words.replaceAllMapped( 364 * pigLatin(String words) => words.replaceAllMapped(
246 * new RegExp(r"\b(\w*?)([aeiou]\w*)", caseSensitive: false), 365 * new RegExp(r"\b(\w*?)([aeiou]\w*)", caseSensitive: false),
247 * (Match m) => "${m[2]}${m[1]}${m[1].isEmpty ? 'way' : 'ay'}"); 366 * (Match m) => "${m[2]}${m[1]}${m[1].isEmpty ? 'way' : 'ay'}");
248 * 367 *
249 * This would convert each word of a text to "pig-latin", so for example 368 * pigLatin("I have a secret now!"); // 'Iway avehay away ecretsay ownay!'
250 * `pigLatin("I have a secret now!")`
251 * returns
252 * `"Iway avehay away ecretsay ownay!"`
253 */ 369 */
254 String replaceAllMapped(Pattern from, String replace(Match match)); 370 String replaceAllMapped(Pattern from, String replace(Match match));
255 371
256 /** 372 /**
257 * Splits the string around matches of [pattern]. Returns 373 * Splits the string around matches of [pattern]. Returns
mem 2013/09/05 23:09:09 around -> at
258 * a list of substrings. 374 * a list of substrings.
259 * 375 *
260 * Splitting with an empty string pattern (`""`) splits at UTF-16 code unit 376 * Splitting with an empty string pattern (`""`) splits at UTF-16 code unit
261 * boundaries and not at rune boundaries. The following two expressions 377 * boundaries and not at rune boundaries:
262 * are hence equivalent:
263 * 378 *
264 * string.split("") 379 * var string = 'Pub';
265 * string.codeUnits.map((unit) => new String.fromCharCode(unit)) 380 * string.split(''); // ['P', 'u', 'b']
381 *
382 * string.codeUnits.map((unit) {
383 * return new String.fromCharCode(unit);
384 * }).toList(); // ['P', 'u', 'b']
385 *
386 * // String made up of two code units, but one rune.
387 * string = "\u{1D11E}";
388 * string.split('').length; // 2
266 * 389 *
267 * Unless it guaranteed that the string is in the basic multilingual plane 390 * Unless it guaranteed that the string is in the basic multilingual plane
mem 2013/09/05 23:09:09 what is "it" here? I'm not sure what this is sayi
shailentuli 2013/09/23 11:50:27 Using some of your's and some of Florian's languag
268 * (meaning that each code unit represents a rune) it is often better to 391 * (meaning that each code unit represents a rune) it is often better to
269 * map the runes instead: 392 * map the runes instead:
270 * 393 *
271 * string.runes.map((rune) => new String.fromCharCode(rune)) 394 * string.runes.map((rune) => new String.fromCharCode(rune));
272 */ 395 */
273 List<String> split(Pattern pattern); 396 List<String> split(Pattern pattern);
274 397
275 /** 398 /**
276 * Splits the string on the [pattern], then converts each part and each match. 399 * Splits the string on the [pattern], then converts each matched and
mem 2013/09/05 23:09:09 on -> at
shailentuli 2013/09/23 11:50:27 Done.
400 * unmatched part.
mem 2013/09/05 23:09:09 converts it to what?
mem 2013/09/05 23:09:09 How about this: Splits the string based on [patte
shailentuli 2013/09/23 11:50:27 Done.
277 * 401 *
278 * The pattern is used to split the string into parts and separating matches. 402 * The pattern is used to split the string into parts and separating matches.
279 * 403 *
280 * Each match is converted to a string by calling [onMatch]. If [onMatch] 404 * Each match is converted to a string by calling [onMatch]. If [onMatch]
281 * is omitted, the matched string is used. 405 * is omitted, the matched string is used.
282 * 406 *
283 * Each non-matched part is converted by a call to [onNonMatch]. If 407 * Each non-matched part is converted by a call to [onNonMatch]. If
284 * [onNonMatch] is omitted, the non-matching part is used. 408 * [onNonMatch] is omitted, the non-matching part is used.
285 * 409 *
286 * Then all the converted parts are combined into the resulting string. 410 * Then all the converted parts are combined into the resulting string.
411 *
412 * 'Eats SHOOTS leaves'.splitMapJoin((new RegExp(r'SHOOTS')),
413 * onMatch: (m) => '*${m.group(0).toLowerCase()}*',
414 * onNonMatch: (n) => n.toUpperCase()); // 'EATS *shoots* LEAVES'
287 */ 415 */
288 String splitMapJoin(Pattern pattern, 416 String splitMapJoin(Pattern pattern,
289 {String onMatch(Match match), 417 {String onMatch(Match match),
290 String onNonMatch(String nonMatch)}); 418 String onNonMatch(String nonMatch)});
291 419
292 /** 420 /**
293 * Returns an unmodifiable list of the UTF-16 code units of this string. 421 * Returns an unmodifiable list of the UTF-16 code units of this string.
294 */ 422 */
295 List<int> get codeUnits; 423 List<int> get codeUnits;
296 424
297 /** 425 /**
298 * Returns an iterable of Unicode code-points of this string. 426 * Returns an iterable of Unicode code-points of this string.
mem 2013/09/05 23:09:09 iterable -> [Iterable]
shailentuli 2013/09/23 11:50:27 Done.
299 * 427 *
300 * If the string contains surrogate pairs, they will be combined and returned 428 * If the string contains surrogate pairs, they are combined and returned
301 * as one integer by this iterator. Unmatched surrogate halves are treated 429 * as one integer by this iterator. Unmatched surrogate halves are treated
302 * like valid 16-bit code-units. 430 * like valid 16-bit code-units.
303 */ 431 */
304 Runes get runes; 432 Runes get runes;
305 433
306 /** 434 /**
307 * If this string is not already all lower case, returns a new string 435 * If this string is not already all lower case, returns a new string
mem 2013/09/05 23:09:09 Put the verb first.... Converts all characters in
shailentuli 2013/09/23 11:50:27 Done.
308 * where all characters are made lower case. Returns [:this:] otherwise. 436 * where all characters are made lower case. Returns [:this:] otherwise.
437 *
438 * 'ALPHABET'.toLowerCase(); // 'alphabet'
439 * 'abc'.toLowerCase(); // 'abc'
309 */ 440 */
310 // TODO(floitsch): document better. (See EcmaScript for description). 441 // TODO(floitsch): document better. (See EcmaScript for description).
311 String toLowerCase(); 442 String toLowerCase();
312 443
313 /** 444 /**
314 * If this string is not already all upper case, returns a new string 445 * If this string is not already all upper case, returns a new string
mem 2013/09/05 23:09:09 same as toLowerCase.
shailentuli 2013/09/23 11:50:27 Done.
315 * where all characters are made upper case. Returns [:this:] otherwise. 446 * where all characters are made upper case. Returns [:this:] otherwise.
447 *
448 * 'alphabet'.toUpperCase(); // 'ALPHABET'
449 * 'ABC'.toUpperCase(); // 'ABC'
316 */ 450 */
317 // TODO(floitsch): document better. (See EcmaScript for description). 451 // TODO(floitsch): document better. (See EcmaScript for description).
318 String toUpperCase(); 452 String toUpperCase();
319 } 453 }
320 454
321 /** 455 /**
322 * The runes (integer Unicode code points) of a [String]. 456 * The runes (integer Unicode code points) of a [String].
323 */ 457 */
324 class Runes extends IterableBase<int> { 458 class Runes extends IterableBase<int> {
325 final String string; 459 final String string;
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 _position = position - 1; 643 _position = position - 1;
510 _currentCodePoint = _combineSurrogatePair(prevCodeUnit, codeUnit); 644 _currentCodePoint = _combineSurrogatePair(prevCodeUnit, codeUnit);
511 return true; 645 return true;
512 } 646 }
513 } 647 }
514 _position = position; 648 _position = position;
515 _currentCodePoint = codeUnit; 649 _currentCodePoint = codeUnit;
516 return true; 650 return true;
517 } 651 }
518 } 652 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698