String
String
Manipulation
Substring
let quote = "The revolution will be Swift"
let substring = quote.dropFirst(23)
let realString = String(substring)
Type conversion to String HWS
Contains
let c = "C"
let objc = "Objective-C"
objc.contains(c)// true
objc.range(of: c, options: .caseInsensitive) != nil
objc.localizedCaseInsensitiveContains(c)
Range
sarunw | different-ways-to-check-if-string-contains-another-string
Printing boolean values in print statement wasn’t straight forward as I believed it was.
So we can encapsulate the boolean value in string and print it.
nth element
Very useful thread for getting character of string in swift, playing with its index values rather than just value of the string character.
let input = "Kautilya Save"
let char = input[input.index(input.startIndex, offsetBy: 0)]
print(char) // K
// Extension method
print(input[0] input[9]) // KS
get-character-from-string-using-its-index-in-swift
You can also use an extension for it. extensions Character nth element
Range
let c = "C"
let objc = "Objective-C"
objc.range(of: capitalC) != nil// true
Computed string _ closure
Pass parametric property like behavior but Swift doesn't allow for parameters to be passed to properties. So we would use function - closure(nameless function) to achieve the same goal.
var image_: (String) -> String {
{ "image-\($0)" }
}
// Call
print(image_("3"))
Expressible Literals
Good article about what they are and how they are used.
avanderlee | expressible-literals
Performance
String concatenation +
is faster than \(string_variable)
in the code according to this below document.
let name = "Kautilya"
let str1: String = "Hi! my name is \(name)"
let str2: String = "Hi! my name is " + name
concatenating-strings-in-swift-which-way-is-faster
Multi Line String
var str1 = """
This goes
over multiple \
lines
"""