Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pauljohanneskraft/5e00f07f5e6dd653cb09e608e2192626 to your computer and use it in GitHub Desktop.
Save pauljohanneskraft/5e00f07f5e6dd653cb09e608e2192626 to your computer and use it in GitHub Desktop.
some String manipulations

String manipulation operators

let fiveTimesHello = 5 * "hello" // "hellohellohellohellohello"
let helloPlus5 = "hello" + 5 // "hello5"
func * (left: UInt, right: String) -> String {
var res = right
res *= left
return res
}
func * (left: String, right: UInt) -> String {
var res = left
res *= right
return res
}
func *= ( left: inout String, right: UInt) -> String {
if right == 0 {
left = ""
return left
}
let orig = left
if right < 5 {
for _ in 1..<right {
left += orig
}
return left
}
let half = right / 2
let m = left * half
left = m + m
if (right & 1 == 1) {
left += orig
}
return left
}
func +<T>(left: String, right: T) -> String {
return "\(left)\(right)"
}
func +<T>(left: T, right: String) -> String {
return "\(left)\(right)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment