create a function to split string in half in swift

To split a string in half in Swift, you can create a function that takes a string as input parameter, calculates the middle point of the string, and returns a tuple containing two substrings - one from the beginning of the original string up to the middle point, and one from the middle point to the end of the original string. Here's an example implementation of such a function in Swift:

func splitStringInHalf(_ string: String) -> (String, String) {
    let middleIndex = string.index(string.startIndex, offsetBy: string.count / 2)
    let firstHalf = String(string[..<middleIndex])
    let secondHalf = String(string[middleIndex...])
    return (firstHalf, secondHalf)
285 chars
7 lines

You can call this function by passing a string as an argument, and it will return a tuple containing the two halves of the string. Here's an example usage:

let myString = "Hello, world!"
let (firstHalf, secondHalf) = splitStringInHalf(myString)
print(firstHalf) // prints "Hello, "
print(secondHalf) // prints "world!"
163 chars
5 lines

gistlibby LogSnag