How to resolve the algorithm Substring step by step in the Swift programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Substring step by step in the Swift programming language
Table of Contents
Problem Statement
Display a substring:
If the program uses UTF-8 or UTF-16, it must work on any valid Unicode code point, whether in the Basic Multilingual Plane or above it. The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16. Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Substring step by step in the Swift programming language
Source code in the swift programming language
let string = "Hello, Swift language"
let (n, m) = (5, 4)
// Starting from `n` characters in and of `m` length.
do {
let start = string.startIndex.advancedBy(n)
let end = start.advancedBy(m)
// Pure-Swift (standard library only):
_ = string[start..<end]
// With Apple's Foundation framework extensions:
string.substringWithRange(start..<end)
}
// Starting from `n` characters in, up to the end of the string.
do {
// Pure-Swift (standard library only):
_ = String(
string.characters.suffix(string.characters.count - n)
)
// With Apple's Foundation framework extensions:
_ = string.substringFromIndex(string.startIndex.advancedBy(n))
}
// Whole string minus last character.
do {
// Pure-Swift (standard library only):
_ = String(
string.characters.prefix(
string.characters.count.predecessor()
)
)
// With Apple's Foundation framework extensions:
_ = string.substringToIndex(string.endIndex.predecessor())
}
// Starting from a known character within the string and of `m` length.
do {
// Pure-Swift (standard library only):
let character = Character("l")
guard let characterIndex = string.characters.indexOf(character) else {
fatalError("Index of '\(character)' character not found.")
}
let endIndex = characterIndex.advancedBy(m)
_ = string[characterIndex..<endIndex]
}
// Starting from a known substring within the string and of `m` length.
do {
// With Apple's Foundation framework extensions:
let substring = "Swift"
guard let range = string.rangeOfString(substring) else {
fatalError("Range of substring \(substring) not found")
}
let start = range.startIndex
let end = start.advancedBy(m)
string[start..<end]
}
You may also check:How to resolve the algorithm Topic variable step by step in the Oforth programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Maple programming language
You may also check:How to resolve the algorithm Nim game step by step in the Clojure programming language