Code Snippet: Check if String Begins with Prefix and Retrieve Remainder

From Coder Merlin
Revision as of 22:39, 5 December 2019 by Yarsenius (talk | contribs) (*Added Python snippet*)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder


Swift[edit]

let s = "Alpha:Bravo:Charlie:Delta"  

let alpha = "Alpha:"
if s.starts(with:alpha) {
    print(s.dropFirst(alpha.count))
}

Python[edit]

s = 'Alpha:Bravo:Charlie:Delta'

alpha = 'Alpha:'
if s.startswith(alpha):
    print(s[len(alpha):])