Sample code for 30+ languages & platforms
Java

StringBuilder SetNth

Demonstrates the SetNth method.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // The SetNth method is handy for setting a part of a delimited string.
        // For example:

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

        String delimiterChar = ",";
        boolean exceptDoubleQuoted = true;
        boolean exceptEscaped = true;

        sb.setNth(2, "magenta", delimiterChar, exceptDoubleQuoted, exceptEscaped);
        // Prints "red,blue,magenta,,yellow"
        System.out.println(sb.getAsString());

        sb.setNth(3, "orange", delimiterChar, exceptDoubleQuoted, exceptEscaped);
        // Prints "red,blue,magenta,orange,yellow"
        System.out.println(sb.getAsString());

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

        sb.setNth(2, "apple", delimiterChar, exceptDoubleQuoted, exceptEscaped);
        // Prints ",,apple"
        System.out.println(sb.getAsString());

        sb.setNth(0, "orange", delimiterChar, exceptDoubleQuoted, exceptEscaped);
        // Prints "orange,,apple"
        System.out.println(sb.getAsString());

        sb.setNth(1, "banana", delimiterChar, exceptDoubleQuoted, exceptEscaped);
        // Prints "orange,banana,apple"
        System.out.println(sb.getAsString());
    }
}