I am trying to insert a character into a string using Processing.
After some reading around I tried out the following (I think Java) code:
1: String st = new String("abcde");
2: st = StringBuffer(st).insert(2, "C");
and got the following response:
the function StringBuffer(String) does not exist
Is there a different/simpler way of doing this? Do I need to use StringBuffer? I’m a fairly novice programmer so any help greatly appreciated.
Ok, so I’ve been looking at the processing ‘Extended Language API’ and there doesn’t seem to be some function like that out of the box.
If you look at the String class’s substring() function, you’ll see an example where there is a String that is cut into two pieces at position 2. And then printed out with other characters between them. Will that help you any further?
If we take your example, this would insert the ‘C’ at the right position:
Or create a function for it. Mind you, not super-robust (no checks for empty strings, overflow etc), but does the job:
tested at http://sketch.processing.org