Strings and Interpolation
Lesson 6 of 48 in Coddy's Swift for Luau Developers course.
Luau backtick interpolation evaluates expressions inside braces. Strings remain byte sequences, and the prefix check below uses byte positions on an ASCII path. string.rep repeats a string; neither interpolation nor repetition changes the indexing unit of the resulting text.
Swift interpolation uses \(...), and hasPrefix expresses the prefix test without manual offsets. String(repeating:count:) constructs repeated text. Swift strings have value semantics and Character-based iteration; byte-level source indexing needs separate consideration for non-ASCII input. Here the ASCII path keeps the formatting comparison direct.
Luau
Complete Luau console program:
main.luau
local folder = "cache"
local path = `{folder}/image`
print(path:sub(1, 6) == "cache/")
print(string.rep(".", 3))
Swift
Complete program; compile the listed files together:
main.swift
import Foundation
let folder = "cache"
let path = "\(folder)/image"
print(path.hasPrefix("cache/"))
print(String(repeating: ".", count: 3))Challenge
EasyImplement badge(_:team:). Input is two ASCII words of 1 to 20 letters: first name then team. Output the team in square brackets, one space, then the name.
Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.
Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.
Try it yourself
import Foundation
func apiChecks(_ input: String) -> Bool {
return badge("Ivo", team: "Oak") == "[Oak] Ivo" && badge("Lee", team: "Pine") == "[Pine] Lee"
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Swift for Luau Developers
1Starting Swift
Working With FilesBindings and MutationNumbers and ConversionBoolean ConditionsRecap: Fare Quote4Functions and Arrays
Functions and LabelsInout and Exclusive AccessTuples and Multiple ResultsArrays Have Value SemanticsRecap: Rising Steps2Text and Decisions
Strings and InterpolationOptionals and BindingGuard and Early ExitIf and Branch ValuesRecap: Entry RulesPractice on your own: Swift playground