Swift Program to check a string starts with a specified substring


Swift provides an hasPrefix() function to check if a string starts with a specified substring. The hasPrefix() function returns a boolean value which indicates whether the specified substring matches the starting string characters of the input string. The hasPrefix() function is case sensitive, which means according to this function “a”, and “A” are two different values.

Input

String = “Ram got first place”
SubString = “Ram”

Output

Yes

Here, the characters of the substring match the starting characters of the input string.

Syntax

func hasPrefix(value)

Here, the value represents a string. This function returns true if the value matches the starting characters of the input string. Otherwise, it will return false. It also returns false if the value is an empty string.

Algorithm

  • Step 1 − Create a string.

  • Step 2 − Create a substring.

  • Step 3 − Use hasPrefix() function to determine whether the given string starts with the specified substring or not.

  • Step 4 − Print the output.

Example 1

In the following Swift program, we will check if a string begins with the given substring. So create a string and two substrings. Then use the hasPrefix() function to check if the specified string starts with the substrings or not. If the string starts with the substring, then print "true”. Otherwise, print “false”.

import Foundation
import Glibc

let myStr = "Ram likes to teach Swift programming language" 
let prefixStr1 = "Ram" 
let prefixStr2 = "Suman"

// Checking if the string begins with the 
// specified substring or not
var res1 = myStr.hasPrefix(prefixStr1)
var res2 = myStr.hasPrefix(prefixStr2)

print("Is '\(myStr)' starts with '\(prefixStr1)' substring?:", res1)
print("Is '\(myStr)' starts with '\(prefixStr2)' substring?:", res2)

Output

Is 'Ram likes to teach Swift programming language' starts with 'Ram' substring?: true
Is 'Ram likes to teach Swift programming language' starts with 'Suman' substring?: false

Example 2

In the following Swift program, we will check if a string begins with the given substring. So create a string and a substring. Then use the hasPrefix() function to determine whether the specified string starts with the substring or not. If the input string begins with the substring, then print "YES! The given string starts with the specified substring.”. Otherwise, print “ NO! The given string does not start with the specified substring."

import Foundation
import Glibc

let myStr = "Learn Swift programming" 
let prefixStr = "Learn" 

// Determine if the string begins with the 
// specified prefix substring or not
if myStr.hasPrefix(prefixStr) {
   print("YES! the given string starts with the specified substring.")
} else {
   print("NO! the given string does not starts with the specified substring.")
}

Output

YES! the given string starts with the specified substring.

Conclusion

So this is how we can check whether a string starts with a specified substring or not using the hasPrefix() function. This method compares every character of the substring with the starting character of the input string. If any of the characters do not match the given string, then it will return false.

Updated on: 09-May-2023

887 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements