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

Print

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.

SO

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

SO

Simple Swift guide

Range

let c = "C"
let objc = "Objective-C"

objc.range(of: capitalC) != nil// true

SO

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
"""

HWS | Code snippet

CFString

// %@    Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise.
String(format: "%@", ["Hello", "world"])
// %%    '%' character.
String(format: "100%% %@", true.description)
// %d, %i    Signed 32-bit integer (int).
String(format: "from %d to %d", Int32.min, Int32.max)
//  %u, %U, %D    Unsigned 32-bit integer (unsigned int).
String(format: "from %u to %u", UInt32.min, UInt32.max)
// %x    Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and lowercase a–f.
String(format: "from %x to %x", UInt32.min, UInt32.max)
// %X    Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F.

String(format: "from %X to %X", UInt32.min, UInt32.max)
// %o, %O    Unsigned 32-bit integer (unsigned int), printed in octal.
String(format: "from %o to %o", UInt32.min, UInt32.max)
// %f    64-bit floating-point number (double), printed in decimal notation. Produces "inf", "infinity", or "nan".

String(format: "from %f to %f", Double.leastNonzeroMagnitude, Double.greatestFiniteMagnitude)

Apple archive | Format specifiers

Copied from SO | Swift string format specifiers

Deprecations

String interpolation Debug description