Strings are fixed length collections of Unicode characters stored as an instance of the class.
|
Most commonly, a string is created from a string literal or by using the constructor on an array of characters:
-
String greeting = "Hello";
|
or
-
char[] bunch = {'H', 'e', 'l', 'l', 'o'};
-
String greeting = new String(bunch);
|
Once created, individual characters of a string cannot be changed in place. This example shows using the methods of indexing, catenation (+), and substring to create a new string with one character changed:
-
String test = "Chicken soup with rice";
-
int a = test.indexOf('w');
-
String newtest = substring(1,a-1)+"is n"+substring(a+5);
-
/* getting "Chicken soup is nice" */
|