Basic HTML version of Foils prepared 18 May 97

Foil 18 The String class

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. by Nancy J. McCracken,Geoffrey C. Fox


1 Strings are fixed length collections of Unicode characters stored as an instance of the class.
2 Most commonly, a string is created from a string literal or by using the constructor on an array of characters:
  • String greeting = "Hello";
3 or
  • char[] bunch = {'H', 'e', 'l', 'l', 'o'};
  • String greeting = new String(bunch);
4 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" */

in Table To:


© 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 Thu Jan 8 1998