HELP! * YELLOW=global GREY=local Global HTML version of Foils prepared 4 February 97

Foil 36 The String class

From Feb 3 Delivered Lecture for Course CPS616 -- Java Lecture 4 -- AWT Through I/O CPS616 spring 1997 -- Feb 3 1997. by Nancy McCracken * See also color IMAGE
Secs 69.1
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" */


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 Sun Feb 16 1997