You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The type `Character` represents a single, human-readable character. Characters are extended grapheme clusters, which consist of one or more Unicode scalars.
40
40
41
-
For example, the single character `ü` can be represented in several ways in Unicode. First, it can be represented by a single Unicode scalar value `ü` ("LATIN SMALL LETTER U WITH DIAERESIS", code point U+00FC). Second, the same single character can be represented by two Unicode scalar values: `u` ("LATIN SMALL LETTER U", code point +0075), and "COMBINING DIAERESIS" (code point U+0308). The combining Unicode scalar value is applied to the scalar before it, which turns a `u` into a `ü`.
41
+
For example, the single character `ü` can be represented in several ways in Unicode. First, it can be represented by a single Unicode scalar value `ü` ("LATIN SMALL LETTER U WITH DIAERESIS", code point U+00FC). Second, the same single character can be represented by two Unicode scalar values: `u` ("LATIN SMALL LETTER U", code point U+0075), and "COMBINING DIAERESIS" (code point U+0308). The combining Unicode scalar value is applied to the scalar before it, which turns a `u` into a `ü`.
42
42
43
43
Still, both variants represent the same human-readable character `ü`:
44
44
@@ -61,11 +61,11 @@ let canadianFlag: Character = "\u{1F1E8}\u{1F1E6}"
61
61
// `canadianFlag` is `🇨🇦`
62
62
```
63
63
64
-
## String templates
64
+
## String templates / String Interpolation
65
65
66
-
String templatesallow constants, variables, and expressions to be inlined into strings simplifying the process of constructing dynamic strings. String templates are currently supported in single-line literals by wrapping the target in parentheses and prefixing it with a backslash (`\`).
66
+
String templates, or string interpolation, allow constants, variables, and expressions to be inlined into strings simplifying the process of constructing dynamic strings. String templates are currently supported in single-line literals by wrapping the target in parentheses and prefixing it with a backslash (`\`).
67
67
68
-
The target in the parentheses must support the built-in function `toString()`, meaning it must evaluate to a `String`, `Number`, `Address`, `Character`, `Bool` or `Path`. Carriage returns, line feeds and nested string literals are not supported inside the parentheses.
68
+
The target in the parentheses must support the built-in function `toString()`, meaning it must evaluate to a `String`, `Number`, `Address`, `Character`, `Bool` or `Path`. Carriage returns, line feeds and nested string literals are not supported inside the parentheses.
69
69
70
70
```cadence
71
71
let result = 2 + 2
@@ -83,151 +83,142 @@ let arr: String = "\(x)"
83
83
84
84
Strings have multiple built-in functions you can use:
85
85
86
-
-
87
-
```cadence
88
-
let length: Int
89
-
```
86
+
-```cadence
87
+
let length: Int
88
+
```
90
89
91
-
Returns the number of characters in the string as an integer.
90
+
Returns the number of characters in the string as an integer.
Concatenates the string `other` to the end of the original string, but does not modify the original string. This function creates a new string whose length is the sum of the lengths of the string the function is called on and the string given as a parameter.
112
+
-```cadence
113
+
view fun concat(_ other: String): String
114
+
```
119
115
120
-
```cadence
121
-
let example = "hello"
122
-
let new = "world"
116
+
Concatenates the string `other` to the end of the original string, but does not modify the original string. This function creates a new string whose length is the sum of the lengths of the string the function is called on and the string given as a parameter.
123
117
124
-
// Concatenate the new string onto the example string and return the new string.
125
-
let helloWorld = example.concat(new)
126
-
// `helloWorld` is now `"helloworld"`
127
-
```
118
+
```cadence
119
+
let example = "hello"
120
+
let new = "world"
128
121
129
-
-
130
-
```cadence
131
-
view fun slice(from: Int, upTo: Int): String
132
-
```
122
+
// Concatenate the new string onto the example string and return the new string.
123
+
let helloWorld = example.concat(new)
124
+
// `helloWorld` is now `"helloworld"`
125
+
```
133
126
134
-
Returns a string slice of the characters in the given string from start index `from` up to, but not including, the end index `upTo`. This function creates a new string whose length is `upTo - from`. It does not modify the original string. If either of the parameters are out of the bounds of the string, or the indices are invalid (`from > upTo`), then the function will fail.
127
+
-```cadence
128
+
view fun slice(from: Int, upTo: Int): String
129
+
```
135
130
136
-
```cadence
137
-
let example = "helloworld"
131
+
Returns a string slice of the characters in the given string from start index `from` up to, but not including, the end index `upTo`. This function creates a new string whose length is `upTo - from`. It does not modify the original string. If either of the parameters are out of the bounds of the string, or the indices are invalid (`from > upTo`), then the function will fail.
138
132
139
-
// Create a new slice of part of the original string.
140
-
let slice = example.slice(from: 3, upTo: 6)
141
-
// `slice` is now `"low"`
133
+
```cadence
134
+
let example = "helloworld"
142
135
143
-
// Run-time error: Out of bounds index, the program aborts.
144
-
let outOfBounds = example.slice(from: 2, upTo: 10)
136
+
// Create a new slice of part of the original string.
137
+
let slice = example.slice(from: 3, upTo: 6)
138
+
// `slice` is now `"low"`
145
139
146
-
// Run-time error: Invalid indices, the program aborts.
147
-
let invalidIndices = example.slice(from: 2, upTo: 1)
148
-
```
149
-
-
150
-
```cadence
151
-
view fun decodeHex(): [UInt8]
152
-
```
140
+
// Run-time error: Out of bounds index, the program aborts.
141
+
let outOfBounds = example.slice(from: 2, upTo: 10)
153
142
154
-
Returns an array containing the bytes represented by the given hexadecimal string.
143
+
// Run-time error: Invalid indices, the program aborts.
144
+
let invalidIndices = example.slice(from: 2, upTo: 1)
145
+
```
155
146
156
-
The given string must only contain hexadecimal characters and must have an even length.
157
-
If the string is malformed, the program aborts.
147
+
-```cadence
148
+
view fun decodeHex(): [UInt8]
149
+
```
158
150
159
-
```cadence
160
-
let example = "436164656e636521"
151
+
Returns an array containing the bytes represented by the given hexadecimal string.
Returns a string where all upper case letters are replaced with lowercase characters.
177
167
178
-
-
179
-
```cadence
180
-
view fun replaceAll(of: String, with: String): String
181
-
```
168
+
```cadence
169
+
let example = "Flowers"
182
170
183
-
Returns a string where all occurences of `of` are replaced with `with`. If `of` is empty, it matches at the beginning of the string and after each UTF-8 sequence yielding k+1 replacements for a string of length k.
171
+
example.toLower() // is `flowers`
172
+
```
184
173
185
-
```cadence
186
-
let example = "abababa"
174
+
-```cadence
175
+
view fun replaceAll(of: String, with: String): String
176
+
```
187
177
188
-
example.replaceAll(of: "a", with: "o") // is `obobobo`
189
-
```
178
+
Returns a string where all occurences of `of` are replaced with `with`. If `of` is empty, it matches at the beginning of the string and after each UTF-8 sequence yielding k+1 replacements for a string of length k.
190
179
191
-
-
192
-
```cadence
193
-
view fun split(separator: String): [String]
194
-
```
180
+
```cadence
181
+
let example = "abababa"
195
182
196
-
Returns the variable-sized array of strings created splitting the receiver string on the `separator`.
183
+
example.replaceAll(of: "a", with: "o") // is `obobobo`
184
+
```
197
185
198
-
```cadence
199
-
let example = "hello world"
186
+
-```cadence
187
+
view fun split(separator: String): [String]
188
+
```
200
189
201
-
example.split(separator: " ") // is `["hello", "world"]`
202
-
```
190
+
Returns the variable-sized array of strings created splitting the receiver string on the `separator`.
203
191
192
+
```cadence
193
+
let example = "hello world"
194
+
195
+
example.split(separator: " ") // is `["hello", "world"]`
196
+
```
204
197
205
198
The `String` type also provides the following functions:
206
199
207
-
-
208
-
```cadence
209
-
view fun String.encodeHex(_ data: [UInt8]): String
210
-
```
200
+
-```cadence
201
+
view fun String.encodeHex(_ data: [UInt8]): String
202
+
```
211
203
212
-
Returns a hexadecimal string for the given byte array
204
+
Returns a hexadecimal string for the given byte array
213
205
214
-
```cadence
215
-
let data = [1 as UInt8, 2, 3, 0xCA, 0xDE]
206
+
```cadence
207
+
let data = [1 as UInt8, 2, 3, 0xCA, 0xDE]
216
208
217
-
String.encodeHex(data) // is `"010203cade"`
218
-
```
209
+
String.encodeHex(data) // is `"010203cade"`
210
+
```
219
211
220
-
-
221
-
```cadence
222
-
view fun String.join(_ strings: [String], separator: String): String
223
-
```
212
+
-```cadence
213
+
view fun String.join(_ strings: [String], separator: String): String
214
+
```
224
215
225
-
Returns the string created by joining the array of `strings` with the provided `separator`.
216
+
Returns the string created by joining the array of `strings` with the provided `separator`.
226
217
227
-
```cadence
228
-
let strings = ["hello", "world"]
229
-
String.join(strings, " ") // is "hello world"
230
-
```
218
+
```cadence
219
+
let strings = ["hello", "world"]
220
+
String.join(strings, separator: " ") // is "hello world"
221
+
```
231
222
232
223
`String`s are also indexable, returning a `Character` value.
233
224
@@ -236,60 +227,56 @@ let str = "abc"
236
227
let c = str[0] // is the Character "a"
237
228
```
238
229
239
-
-
240
-
```cadence
241
-
view fun String.fromUTF8(_ input: [UInt8]): String?
242
-
```
230
+
-```cadence
231
+
view fun String.fromUTF8(_ input: [UInt8]): String?
232
+
```
243
233
244
-
Attempts to convert a UTF-8 encoded byte array into a `String`. This function returns `nil` if the byte array contains invalid UTF-8,
245
-
such as incomplete codepoint sequences or undefined graphemes.
234
+
Attempts to convert a UTF-8 encoded byte array into a `String`. This function returns `nil` if the byte array contains invalid UTF-8,
235
+
such as incomplete codepoint sequences or undefined graphemes.
246
236
247
-
For a given string `s`, `String.fromUTF8(s.utf8)` is equivalent to wrapping `s` up in an [optional].
237
+
For a given string `s`, `String.fromUTF8(s.utf8)` is equivalent to wrapping `s` up in an [optional].
248
238
249
239
## Character fields and functions
250
240
251
241
`Character` values can be converted into `String` values using the `toString` function:
252
242
253
-
-
254
-
```cadence
255
-
view fun toString(): String`
256
-
```
243
+
-```cadence
244
+
view fun toString(): String`
245
+
```
257
246
258
-
Returns the string representation of the character.
247
+
Returns the string representation of the character.
259
248
260
-
```cadence
261
-
let c: Character = "x"
249
+
```cadence
250
+
let c: Character = "x"
262
251
263
-
c.toString() // is "x"
264
-
```
252
+
c.toString() // is "x"
253
+
```
265
254
266
-
-
267
-
```cadence
268
-
view fun String.fromCharacters(_ characters: [Character]): String
269
-
```
255
+
-```cadence
256
+
view fun String.fromCharacters(_ characters: [Character]): String
257
+
```
270
258
271
-
Builds a new `String` value from an array of `Character`s. Because `String`s are immutable, this operation makes a copy of the input array.
259
+
Builds a new `String` value from an array of `Character`s. Because `String`s are immutable, this operation makes a copy of the input array.
272
260
273
-
```cadence
274
-
let rawUwU: [Character] = ["U", "w", "U"]
275
-
let uwu: String = String.fromCharacters(rawUwU) // "UwU"
276
-
```
261
+
```cadence
262
+
let rawUwU: [Character] = ["U", "w", "U"]
263
+
let uwu: String = String.fromCharacters(rawUwU) // "UwU"
264
+
```
277
265
278
-
-
279
-
```cadence
280
-
let utf8: [UInt8]
281
-
```
266
+
-```cadence
267
+
let utf8: [UInt8]
268
+
```
282
269
283
-
The byte array of the UTF-8 encoding.
270
+
The byte array of the UTF-8 encoding.
284
271
285
-
```cadence
286
-
let a: Character = "a"
287
-
let a_bytes = a.utf8 // `a_bytes` is `[97]`
272
+
```cadence
273
+
let a: Character = "a"
274
+
let a_bytes = a.utf8 // `a_bytes` is `[97]`
288
275
289
-
let bouquet: Character = "\u{1F490}"
290
-
let bouquet_bytes = bouquet.utf8 // `bouquet_bytes` is `[240, 159, 146, 144]`
291
-
```
276
+
let bouquet: Character = "\u{1F490}"
277
+
let bouquet_bytes = bouquet.utf8 // `bouquet_bytes` is `[240, 159, 146, 144]`
278
+
```
292
279
293
280
<!-- Relative links. Will not render on the page -->
0 commit comments