Sample code for 30+ languages & platforms
Lua

StringBuilder SetNth

Demonstrates the SetNth method.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- The SetNth method is handy for setting a part of a delimited string.
-- For example:

local sb = chilkat.StringBuilder.new()
sb:append("red,blue,\"green,purple\",,yellow")

local delimiterChar = ","
local exceptDoubleQuoted = true
local exceptEscaped = true

sb:set_nth(2, "magenta", delimiterChar, exceptDoubleQuoted, exceptEscaped)
-- Prints "red,blue,magenta,,yellow"
print(sb:get_as_string())

sb:set_nth(3, "orange", delimiterChar, exceptDoubleQuoted, exceptEscaped)
-- Prints "red,blue,magenta,orange,yellow"
print(sb:get_as_string())

-- What happens if we start with an empty string?
sb:clear()

sb:set_nth(2, "apple", delimiterChar, exceptDoubleQuoted, exceptEscaped)
-- Prints ",,apple"
print(sb:get_as_string())

sb:set_nth(0, "orange", delimiterChar, exceptDoubleQuoted, exceptEscaped)
-- Prints "orange,,apple"
print(sb:get_as_string())

sb:set_nth(1, "banana", delimiterChar, exceptDoubleQuoted, exceptEscaped)
-- Prints "orange,banana,apple"
print(sb:get_as_string())