Strings are fixed-length collections of Unicode characters.
|
Usually 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. The following example uses String methods to create a new string:
-
String test = "Chicken soup with rice";
-
int n = test.indexOf( 'w' );
-
String newtest = test.substring(1,n-1) + "is n" + test.substring(n+5);
-
/* giving "Chicken soup is nice" */
|