
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
Check if string contains another string in Swift
To check if a string contains another string in swift, we’ll need two different strings. One string that we have to check if it consists another string.
Let us say the string we want to check is “point” and the whole string is “TutorialsPoint” and another string is “one two three”. Let’s check with both these string in the playground.
We can do this in two ways as shown below. Let’s start by creating three different strings.
var CompleteStr1 = "Tutorials point" var completeStr2 = "one two three" var stringToCheck = "point"
Method One
In this method we’ll use the .contains method of Strings to check if there is a string within another string, it returns true if it exists, otherwise, it returns false.
if CompleteStr1.contains(stringToCheck) { print("contains") } else { print("does not contain") }
Method Two
In this method we’ll check the range of a string if the range is nil, it means that the string we are checking for, does not exist. Otherwise, it means that string exists.
if completeStr2.range(of: stringToCheck) != nil { print("contains") } else { print("does not contain") }
When we run the above code, we get the output as shown below.
Similarly, let’s try these methods with one more example.
var Str1 = "12312$$33@" var Str2 = "%%" var Str3 = "$$" if Str1.contains(Str2) { print("contains") } else { print("does not contain") } if Str1.range(of: Str3) != nil { print("contains") } else { print("does not contain") }
This produces the result as shown below.
- Related Articles
- Check if string contains special characters in Swift
- How to check if a String contains another String in a case insensitive manner in Java?
- Check if a string contains a sub-string in C++
- How do I determine if a String contains another String in Java?
- Check If a String Can Break Another String in C++
- Check if a string contains numbers in MySQL?
- How to check if a string contains a specific sub string?
- Check if a field contains a string in MongoDB?
- Check if a String Contains a Substring in Linux
- Check if a string can be repeated to make another string in Python
- Check if the String contains only unicode letters in Java
- Method to check if a String contains a sub string ignoring case in Java
- Check if a string contains a palindromic sub-string of even length in Python
- Check if a string contains a palindromic sub-string of even length in C++
- Swift Program to Insert a string into another string
