Swift Program to Check if a Variable is String

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program check if a variable is 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 an is operator to check if the given variable of String type or not. If the given variable of String type, then it will return true. Otherwise, it will return false.

Below is a demonstration of the same −

Suppose our given input is −

MyVal = "tutorials point"

The desired output would be −

Yes! MyVal is of String type

Suppose our given input is −

MyVal = 23.3

The desired output would be −

No! MyVal is not of String type

Syntax

Following is the syntax 

variableName is String

Algorithm

Following is the algorithm −

  • Step 1 − Declare a variable with value

  • Step 2 − Check if the given variable is of string type or not using is operator.

OriString is String
  • Step 3 − Print the output

Example 1

The following program shows how to check if a variable is String.

import Foundation import Glibc var OriString = "You are a very good footballer" // Checking if the given variable is // of String type or not if (OriString is String){ print("YES! OriString is of String type") } else{ print("NO! OriString is not of String type") }

Output

YES! OriString is of String type
main.swift:8:15: warning: 'is' test is always true
if (OriString is String){
^
main.swift:12:11: warning: will never be executed
print("NO! OriString is not of String type")
^
main.swift:8:15: note: condition always evaluates to true
if (OriString is String){
^

Note − It will show warning which can be ignore.

Here, in the above code, we have a variable named OriString. Now we check if this variable is of String type or not using is operator −

OriString is String

Here we get "YES! OriString is of String type" as an output because OriString variable is of String type.

Example 2

The following program shows how to check if a variable is String.

import Foundation import Glibc var value1 = "You are a very good footballer" var value2 = 343 var value3 = "4839" // Checking if the given variables are // of String type or not var res1 = value1 is String var res2 = value2 is String var res3 = value3 is String print("Is value1 is of String Type:", res1) print("Is value2 is of String Type:", res2) print("Is value3 is of String Type:", res3)

Output

Is value1 is of String Type: true
Is value2 is of String Type: false
Is value3 is of String Type: true
main.swift:10:19: warning: 'is' test is always true
var res1 = value1 is String
^
main.swift:11:19: warning: cast from 'Int' to unrelated type 'String' always fails
var res2 = value2 is String
~~~~~~ ^ ~~~~~~
main.swift:12:19: warning: 'is' test is always true
var res3 = value3 is String

Note − It will show warning which can be ignore.

Here, in the above code, we have three variables named value1, value2, and value1. Now we check if these variables are of String type or not using is operator −

var res1 = value1 is String // Return true
var res2 = value2 is String // Return false
var res3 = value3 is String // Return true
raja
Updated on 11-Oct-2022 11:48:33

Advertisements