I could not find a method that adds a character in a String to a given position. Not in a String array.
|
1 answer
The String class implements immutable strings, so there are no methods in it that modify the original string "in place" (do not confuse with such methods as String::replace() - they return a new String object as the result of some processing of the original string). But suitable methods like insert() are in such classes as StringBuffer and StringBuilder - they implement a variable string.
A simple code example:
public class Example { public static void main(String[] args) { String s = "test string"; String new_s = (new StringBuilder(s)).insert(5, "(inserted) ").toString(); System.out.println(new_s); } } |