Sample code for 30+ languages & platforms
Swift

StringBuilder SetNth

Demonstrates the SetNth method.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    // The SetNth method is handy for setting a part of a delimited string.
    // For example:

    let sb = CkoStringBuilder()!
    sb.append(value: "red,blue,\"green,purple\",,yellow")

    var delimiterChar: String? = ","
    var exceptDoubleQuoted: Bool = true
    var exceptEscaped: Bool = true

    sb.setNth(index: 2, value: "magenta", delimiterChar: delimiterChar, exceptDoubleQuoted: exceptDoubleQuoted, exceptEscaped: exceptEscaped)
    // Prints "red,blue,magenta,,yellow"
    print("\(sb.getAsString()!)")

    sb.setNth(index: 3, value: "orange", delimiterChar: delimiterChar, exceptDoubleQuoted: exceptDoubleQuoted, exceptEscaped: exceptEscaped)
    // Prints "red,blue,magenta,orange,yellow"
    print("\(sb.getAsString()!)")

    // What happens if we start with an empty string?
    sb.clear()

    sb.setNth(index: 2, value: "apple", delimiterChar: delimiterChar, exceptDoubleQuoted: exceptDoubleQuoted, exceptEscaped: exceptEscaped)
    // Prints ",,apple"
    print("\(sb.getAsString()!)")

    sb.setNth(index: 0, value: "orange", delimiterChar: delimiterChar, exceptDoubleQuoted: exceptDoubleQuoted, exceptEscaped: exceptEscaped)
    // Prints "orange,,apple"
    print("\(sb.getAsString()!)")

    sb.setNth(index: 1, value: "banana", delimiterChar: delimiterChar, exceptDoubleQuoted: exceptDoubleQuoted, exceptEscaped: exceptEscaped)
    // Prints "orange,banana,apple"
    print("\(sb.getAsString()!)")

}