Basic HTML version of Foils prepared 17 Nov 97

Foil 21 The String Class

From Java Tutorial, July 1, 1996 CEWES Tutorial, CPS606, JSU Class CSC499 -- July 22-25 1997, Fall 97. by Nancy J. McCracken,Geoffrey C. Fox, Tom Scavo


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" */



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed Apr 1 1998