diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-01-05 19:26:02 -0500 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-01-05 19:26:02 -0500 |
commit | 30ef32e238465e67321d15c562d6313043dd12d7 (patch) | |
tree | 191aea4160763f196735377d0db240bb50fb7c7a /06_strings.zig | |
parent | faa49abb068adca4ecc6c00dfe72d0674a763217 (diff) | |
download | ziglings-30ef32e238465e67321d15c562d6313043dd12d7.tar.gz ziglings-30ef32e238465e67321d15c562d6313043dd12d7.tar.bz2 ziglings-30ef32e238465e67321d15c562d6313043dd12d7.tar.xz ziglings-30ef32e238465e67321d15c562d6313043dd12d7.zip |
Added Exs. 6,7 strings
Diffstat (limited to '06_strings.zig')
-rw-r--r-- | 06_strings.zig | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/06_strings.zig b/06_strings.zig new file mode 100644 index 0000000..cac40e0 --- /dev/null +++ b/06_strings.zig @@ -0,0 +1,40 @@ +// +// Now that we've learned about arrays, we can talk about strings. +// +// We've already seen Zig string literals: "Hello world.\n" +// +// Like the C language, Zig stores strings as arrays of bytes +// encoded as UTF-8 characters terminated with a null value. +// For now, just focus on the fact that strings are arrays of +// characters! +// +const std = @import("std"); + +pub fn main() void { + const ziggy = "stardust"; + + // Use array square bracket syntax to get the letter 'd' from + // the string "stardust" above. + const d: u8 = ziggy[???]; + + // Use the array repeat '**' operator to make "ha ha ha". + const laugh = "ha " ???; + + // Use the array concatenation '++' operator to make "Major Tom". + // (You'll need to add a space as well!) + const major = "Major"; + const tom = "Tom"; + const major_tom = major ??? tom; + + std.debug.print("d={u} {}{}\n",.{d, laugh, major_tom}); + // Going deeper: + // Keen eyes will notice that we've put a 'u' inside the '{}' + // placeholder in the format string above. This tells the + // print() function (which uses std.fmt.format() function) to + // print out a UTF-8 character. Otherwise we'd see '100', which + // is the decimal number corresponding with the 'd' character + // in UTF-8. + // While we're on this subject, 'c' (ASCII encoded character) + // would work in place for 'u' because the first 128 characters + // of UTF-8 are the same as ASCII! +} |