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

From Coder Merlin
Jump to navigation Jump to search

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):])


CoderMerlin™ proudly recommends:
HintIcon.png

Your purchase via these links helps keep  Coder Merlin™ 's orbuculum ablaze. As an Amazon Associate we earn from qualifying purchases. Thank you for your support!


Designed with pride in Silicon Valley, CA, USA