Swift Substring
To find substring of a String in Swift, prepare Range object using start and end indices, then give this Range object to the string in square brackets as an index.
Syntax
The syntax to concatenate two strings is:
let start = str.index(str.startIndex, offsetBy: startPosition)
let end = str.index(str.endIndex, offsetBy: -endPosition)
let range = start..<end
let subStr = str[range]
where startPosition
and endPosition
are integers that define the bounds of the substring in the main string str
.
startPosition
is the index (from starting) in the main string from which substring has to be confirmed.
endPosition
is the index (from ending) in the main string.
Rest all are keywords or functions or constants. Finally subStr
holds the substring.
You might be wondering, why all this hassle to find a substring! One possible theory is that Swift Characters being composed of varying number of Unicode codepoints. The actual index has to be uniquely calculated for every string.
Examples
This example demonstrates how to find the substring of a string. The starting position is taken as 2
and the ending position is taken as 4
.
main.swift
var str = "TutorialKart"
let start = str.index(str.startIndex, offsetBy: 2)
let end = str.index(str.endIndex, offsetBy: -4)
let range = start..<end
let subStr = str[range]
print( subStr )
Output
torial
Example 2 – Substring of a String with startPosition 0
In this example, the starting position is 0
, hence the substring starts from the start of main string and ends at the position defined by ending index. Ending index is 4
in this example.
main.swift
var str = "TutorialKart"
let start = str.index(str.startIndex, offsetBy: 0)
let end = str.index(str.endIndex, offsetBy: -4)
let range = start..<end
let subStr = str[range]
print( subStr )
Output
Tutorial
Example 3 – Substring of a String with endPosition 0
In this example, the ending position is 0
, hence the substring ends at the end of main string. However the start position is defined by starting index. Starting index is 5
in this example.
main.swift
var str = "TutorialKart"
let start = str.index(str.startIndex, offsetBy: 5)
let end = str.index(str.endIndex, offsetBy: 0)
let range = start..<end
let subStr = str[range]
print( subStr )
Output
ialKart
Example 4 – Substring of a String with startPosition greater than String length
Sometimes, it can happen that we provide start position or end position exceeding string length.
In this example, we provided starting position greater than the length of the string. Hence the Fatal error with the message: cannot increment beyong endIndex.
main.swift
var str = "TutorialKart"
let start = str.index(str.startIndex, offsetBy: 25)
let end = str.index(str.endIndex, offsetBy: 0)
let range = start..<end
let subStr = str[range]
print( subStr )
Output
Fatal error: String index is out of bounds: file Swift/StringCharacterView.swift, line 60
2021-02-12 09:30:02.489899+0530 HelloWorld[1072:40084] Fatal error: String index is out of bounds: file Swift/StringCharacterView.swift, line 60
Printing description of range:
▿ Range(Swift.String.Index(_rawBits: 0)..<Swift.String.Index(_rawBits: 0))
▿ lowerBound : Index
- _rawBits : 0
▿ upperBound : Index
- _rawBits : 0
(lldb)
Conclusion
In this Swift Tutorial, we have learnt how to find substring of a string in Swift with the help of syntax and examples.