The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

## Summary of Strings

> The following table summarizes the methods found in the `String` and `StringBuffer` classes.

### Summary of the String Class

> Method or Constructor | Purpose
> --- | ---
> `String() String (byte[]) String (byte[], int, int) String (byte[], int, int, String) String (byte[], String) String (char[]) String (char[], int, int) String (String) String (StringBuffer)` | Creates a new string object. For all constructors that take arguments, the first argument provides the value for the string.

### Other Interesting Features

> `String` and `StringBuffer` provide several other useful ways to manipul ate string data, including concatenation, comparison, substitution, and conversion to upper and lower case. `java.lang.String` and `java.lang.StringBuffer` summarize and list all of the methods and variables supported by these two classes.

> Here's a fun program, `Palindrome`, that determines whether a string is a palindrome. This program uses many methods from the `String` and the `StringBuffer` classes:

> >     public class Palindrome {
> >         public static boolean isPalindrome(String stringToTest) {
> >             String workingCopy = removeJunk(stringToTest);
> >             String reversedCopy = reverse(workingCopy);

> >             return reversedCopy.equalsIgnoreCase(workingCopy);
> >         }

> >         protected static String removeJunk(String string) {
> >             int i, len = string.length();
> >             StringBuffer dest = new StringBuffer(len);
> >             char c;

> >             for (i = (len - 1); i >= 0; i--) {
> >                 c = string.charAt(i);
> >                 if (Character.isLetterOrDigit(c)) {
> >                     dest.append(c);
> >                 }
> >             }

> >             return dest.toString();
> >         }

> >         protected static String reverse(String string) {
> >             StringBuffer sb = new StringBuffer(string);

> >             return sb.reverse().toString();
> >         }

> >         public static void main(String[] args) {
> >             String string = "Madam, I'm Adam.";

> >             System.out.println();
> >             System.out.println("Testing whether the following "
> >                                + "string is a palindrome:");
> >             System.out.println("    " + string);
> >             System.out.println();

> >             if (isPalindrome(string)) {
> >                 System.out.println("It IS a palindrome!");
> >             } else {
> >                 System.out.println("It is NOT a palindrome!");
> >             }
> >             System.out.println();
> >         }
> >     }

> The output from this program is:

> >     Testing whether the following string is a palindrome:
> >         Madam, I'm Adam.

> >     It IS a palindrome!

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.