Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 1.32 KB

Strings.md

File metadata and controls

47 lines (35 loc) · 1.32 KB

Strings

Substrings

Substrings are tricky. You can't just subscript. You first need to get an index (type), and then use that index to pull out the substring you want.

Here is an example of how to segment a string by walking it one character at a time and breaking it into two words.

func canSegmentString(s: String, arr: [String]) -> Bool {
    for i in 0..<s.count {
        let indexStartOfText = s.index(s.startIndex, offsetBy: i)
        let indexEndOfText = s.index(s.endIndex, offsetBy: -i)
        
        let firstWord = s[..<indexStartOfText]
        let secondWord = s[indexStartOfText...]
        print("\(firstWord) \(secondWord)")
    }
    return false
}

canSegmentString(s: "applepear", arr: ["apple", "pear"])

 applepear
a pplepear
ap plepear
app lepear
appl epear
apple pear
applep ear
applepe ar
applepea r

Non-breaking hyphen

let nonBreakingHyphen = "\u{2011}"
subtitle: "Interac e\(Constants.nonBreakingHyphen)Transfer®",

Links that help