Basic HTML version of Foils prepared Sept 21 1998

Foil 21 The String Class

From Java Tutorial 98- 4: Multi-Treading, Useful Java Classes, I/O and Networking NAVO Tutorial -- Sept 23 1998. by Geoffrey C. Fox, Nancy McCracken


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 Sat Nov 28 1998