Write a Swift program to test if a given string starts with “test”

Topics

Introduction

I have used Swift for windows 1.9 for debugging purpose. But you can use any Swift programming language compiler as per your availability.

import Foundation
 
func checkstring(_ input: String) -> Bool {
    if input.hasPrefix("test")
    {
        return true
    } 
    else 
    {
        return false
    }
}
print(checkstring("testpqrsab"))
print(checkstring("testabpqrsab"))
print(checkstring("pqrs"))
print(checkstring("abpqrs"))

Result

Write a Swift program to test if a given string starts with "test"
Write a Swift program to test if a given string starts with “test”

Leave a Comment