1 |
Strings are fixed-length collections of Unicode characters.
|
2 |
Usually a string is created from a string literal or by using the constructor on an array of characters:
|
3 |
String greeting = "Hello";
|
4 |
or
|
5 |
char[] bunch = {'H', 'e', 'l', 'l', 'o'};
|
6 |
String greeting = new String( bunch );
|
7 |
Once created, individual characters of a string cannot be changed in place. The following example uses String methods to create a new string:
|
8 |
String test = "Chicken soup with rice";
|
9 |
int n = test.indexOf( 'w' );
|
10 |
String newtest = test.substring(1,n-1) +
-
"is n" + test.substring(n+5);
-
/* giving "Chicken soup is nice" */
|