Swift Program to Check if a String Starts With the Given String

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to check if a String starts with the given string.

A string is an ordered set of characters, for example, "TutorialsPoint". It is used to represent textual data. Swift support a String data type which is used to create a string type variable, or we can say to represent strings.

Swift provide a in-built function named starts() to check whether the string starts with the specified string or not. If the given string starts with specified string, then it will return true. Otherwise, it will return false.

Below is a demonstration of the same −

Suppose our given input is −

MyVal = "Moon look beautiful in every shape"
String = "Moon"

The desired output would be −

YES! The given string starts with "Moon" string.

Suppose our given input is −

MyVal = "Moon look beautiful in every shape"
String = "Sky"

The desired output would be −

No! The given string does not starts with "Sky" string.

Syntax

Following is the syntax −

string.starts(with:str)

Algorithm

Following is the algorithm −

  • Step 1 − Declare a variable of String type with value.

  • Step 2 − Declare another variable of String type with value.

  • Step 3 − Check if the given string starts with the specified string or not using starts() function −

myString.starts(with:value)
  • Step 4 − Print the output

Example 1

The following program shows how to check if a String starts with the given string.

import Foundation import Glibc var myString = "Always stay blessed my friend" var value = "Always" print("String =",myString) print("Does it start with the following word") print(value) // Checking if the given string starts // with the specified string or not if (myString.starts(with:value) == true) { print("YES! myString starts with \(value)") } else{ print("NO! myString does not starts with \(value)") }

Output

String = Always stay blessed my friend
Does it start with the following word
Always
YES! myString starts with Always

Here, in the above code, we have a string named myString = "Always stay blessed my friend". Now we check myString starts with "Always" or not with the help of starts() function. Hence we get "YES! myString starts with Always" because myString starts with Always.

Example 2

The following program shows how to check if a String starts with the given string.

import Foundation import Glibc var myString = "Today is a rainy day" print("String =",myString) var value1 = "Today" var value2 = "It" // Checking if the given string starts // with the specified string or not var res1 = myString.starts(with:value1) var res2 = myString.starts(with:value2) print("Is myString starts with \(value1)?:", res1) print("Is myString starts with \(value2)?:", res2)

Output

String = Today is a rainy day
Is myString starts with Today?: true
Is myString starts with It?: false

Here, in the above code, we have a string named myString = "Today is a rainy day". Now we check myString starts with "Today" and "It" with the help of starts() function −

var res1 = myString.starts(with:value1) // Return true
var res2 = myString.starts(with:value2) // Return false
raja
Updated on 11-Oct-2022 11:53:58

Advertisements