w3resource

Swift Basic Programming Exercise: Test a given string whether it starts with "Is"


Write a Swift program to test a given string whether it starts with "Is". Return true or false.

Pictorial Presentation:

Swift Basic Programming Exercise: Test a given string whether it starts with 'Is'.

Sample Solution:

Swift Code:

func start_is(str1: String) -> Bool {
    let str2 = str1.characters
    let hello = "Is"
    let first2Values = str2.prefix(2)
    let first_two = String(first2Values)
    
    if hello == first_two {
        return true
    } else {
        return false
    }
}


print(start_is(str1: "Is Swift"))
print(start_is(str1: "is python"))
print(start_is(str1: "java is"))

Sample Output:

true
false
false

Go to:


PREV : Write a Swift program to take the first two characters from a given string and create a new string with the two characters added at both the front and back.
NEXT : Write a Swift program that return true if either of two given integers is in the range 10..30 inclusive.

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.