@@ -1260,6 +1260,113 @@ got codepoint E382AB
1260
1260
```
1261
1261
1262
1262
1263
+ ### Some useful functions for strings
1264
+
1265
+ In this section, I just want to quickly describe some functions from the Zig Standard Library
1266
+ that are very useful to use when working with strings. Most notably:
1267
+
1268
+ - ` std.mem.eql() ` : to compare if two strings are equal.
1269
+ - ` std.mem.splitScalar() ` : to split a string into an array of substrings given a delimiter value.
1270
+ - ` std.mem.splitSequence() ` : to split a string into an array of substrings given a substring delimiter.
1271
+ - ` std.mem.startsWith() ` : to check if string starts with substring.
1272
+ - ` std.mem.endsWith() ` : to check if string starts with substring.
1273
+ - ` std.mem.trim() ` : to remove specific values from both start and end of the string.
1274
+ - ` std.mem.concat() ` : to concatenate strings together.
1275
+ - ` std.mem.count() ` : to count the occurrences of substring in the string.
1276
+ - ` std.mem.replace() ` : to replace the occurrences of substring in the string.
1277
+
1278
+ Notice that all of these functions come from the ` mem ` module of
1279
+ the Zig Standard Library. This module contains multiple functions and methods
1280
+ that are useful to work with memory and sequences of bytes in general.
1281
+
1282
+ The ` eql() ` function is used to check if two arrays of data are equal or not.
1283
+ Since strings are just arbitrary arrays of bytes, we can use this function to compare two strings together.
1284
+ This function returns a boolean value indicating if the two strings are equal
1285
+ or not. The first argument of this function is the data type of the elements of the arrays
1286
+ that are being compared.
1287
+
1288
+ ``` {zig}
1289
+ #| auto_main: true
1290
+ #| build_type: "run"
1291
+ const name: []const u8 = "Pedro";
1292
+ try stdout.print(
1293
+ "{any}\n", .{std.mem.eql(u8, name, "Pedro")}
1294
+ );
1295
+ ```
1296
+
1297
+ The ` splitScalar() ` and ` splitSequence() ` functions are useful to split
1298
+ a string into multiple fragments, like the ` split() ` method from Python strings. The difference between these two
1299
+ methods is that the ` splitScalar() ` uses a single character as the separator to
1300
+ split the string, while ` splitSequence() ` uses a sequence of characters (a.k.a. a substring)
1301
+ as the separator. There is a practical example of these functions later in the book.
1302
+
1303
+ The ` startsWith() ` and ` endsWith() ` functions are pretty straightforward. They
1304
+ return a boolean value indicating if the string (or, more precisely, if the array of data)
1305
+ begins (` startsWith ` ) or ends (` endsWith ` ) with the sequence provided.
1306
+
1307
+ ``` {zig}
1308
+ #| auto_main: true
1309
+ #| build_type: "run"
1310
+ const name: []const u8 = "Pedro";
1311
+ try stdout.print(
1312
+ "{any}\n", .{std.mem.startsWith(u8, name, "Pe")}
1313
+ );
1314
+ ```
1315
+
1316
+ The ` concat() ` function, as the name suggests, concatenate two or more strings together.
1317
+ Because the process of concatenating the strings involves allocating enough space to
1318
+ accomodate all the strings together, this ` concat() ` function receives an allocator
1319
+ object as input.
1320
+
1321
+ ``` {zig}
1322
+ #| eval: false
1323
+ #| auto_main: true
1324
+ #| build_type: "run"
1325
+ const str1 = "Hello";
1326
+ const str2 = " you!";
1327
+ const str3 = try std.mem.concat(
1328
+ allocator, u8, &[_][]const u8{ str1, str2 }
1329
+ );
1330
+ try stdout.print("{s}\n", .{str3});
1331
+ ```
1332
+
1333
+ ```
1334
+ Hello you!
1335
+ ```
1336
+
1337
+ As you can imagine, the ` replace() ` function is used to replace substrings in a string by another substring.
1338
+ This function works very similarly to the ` replace() ` method from Python strings. Therefore, you
1339
+ provide a substring to search, and every time that the ` replace() ` function finds
1340
+ this substring within the input string, it replaces this substring with the "replacement substring"
1341
+ that you provided as input.
1342
+
1343
+ In the example below, we are taking the input string "Hello", and replacing all occurrences
1344
+ of the substring "el" inside this input string with "34", and saving the results inside the
1345
+ ` buffer ` object. As result, the ` replace() ` function returns an ` usize ` value that
1346
+ indicates how many replacements were performed.
1347
+
1348
+
1349
+ ``` {zig}
1350
+ #| auto_main: true
1351
+ #| build_type: "lib"
1352
+ const str1 = "Hello";
1353
+ var buffer: [5]u8 = undefined;
1354
+ const nrep = std.mem.replace(
1355
+ u8, str1, "el", "34", buffer[0..]
1356
+ );
1357
+ try stdout.print("New string: {s}\n", .{buffer});
1358
+ try stdout.print("N of replacements: {d}\n", .{nrep});
1359
+ ```
1360
+
1361
+ ```
1362
+ New string: H34lo
1363
+ N of replacements: 1
1364
+ ```
1365
+
1366
+
1367
+
1368
+
1369
+
1263
1370
1264
1371
## Safety in Zig
1265
1372
0 commit comments