Using String Classes

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 10.  Java Classes


Using String Classes

Java provides support for strings using the String class. A String is a collection of characters that presumably represents some meaningful value. Characters are delimited by single quotes, but a string is delimited by double quotes. For example:

 String name = "Michael"; 

Java manages strings differently than other primitive data types. It maintains a string table, which holds all strings that are in memory at any given time. Similar to the wrapper classes, the String class is immutable, which means you cannot modify the value of the string, but you must create a new string to hold a new value. If you create a new string matching a value that is already in the string table, your string will simply reference the existing one.

Java implemented this as an optimization to minimize the duplication of string values that tend to exist so frequently in software programs. So how does this affect you?

From the surface this appears to be a nice optimization provided by Java, but insignificant to the programmer. Most beginning Java programmers do not understand that strings are immutable, nor do they know of the existence of the string table.

The impact of string immutability is that the modification of the string actually results in the creation of a new entry into the string table. The String class is special because the addition operator is overloaded. For example:

 String greeting = "Hello";  greeting += ", ";  greeting += "World!";  System.out.println( greeting ); 

If you were to run this code snippet, it would print out:

 Hello, World! 

This is exactly what you'd expect, but what is happening in memory? If we applied the principles of immutability combined with the concept of a string table to the string greeting, there must be a new "Hello" entry added to the string table on the creation of the greeting variable. When the comma is appended to the greeting, the original "Hello" string cannot be modified, so there must be a new entry added to the string table with the value "Hello, ". Furthermore, with the addition of the "World!" string, there must be yet another string created and added to the string table; this time with the value "Hello, World!". Thus, the string table's memory has been littered with two unused strings. It must have the following values:

 Hello  Hello,  Hello, World 

Eventually the garbage collector will clean the orphaned memory, but this program inadvertently generated the need to do so. This example might not seem extreme, but consider the following pseudo code that reads a text file character by character:

 String value;  while( notEndOfFile ) {    value += nextCharacter;  } 

And also consider that the file looks as follows:

 abcdefg 

The resultant string table will look as follows:

 a  ab  abc  abcd  abcde  abcdef  abcdefg 

What if the file were several hundred kilobytes? Now this becomes a significant problem. Because of the limitation of immutable strings, Java has provided a class to manipulate strings in memory that bypasses the string table the StringBuffer class. The StringBuffer class maintains a collection of characters and enables you to insert and append characters or strings to it. You can build a StringBuffer from a String, and you can convert a StringBuffer to a String. Therefore, it is very typical to see a string manipulated by using the StringBuffer class. Between the String class and the StringBuffer class, you will find most every function is string manipulation that you could want.

The String Class

The String class provides methods to help you manipulate and modify strings (see Table 10.18).

Table 10.18. String Method Summary

Method

Description

char charAt(int index)

Returns the character at the specified index.

int compareTo(Object o)

Compares this String to another Object.

int compareTo(String anotherString)

Compares two strings lexicographically.

int compareToIgnoreCase(String str)

Compares two Strings lexicographically, ignoring case considerations.

String concat(String str)

Concatenates the specified String to the end of this String.

static String copyValueOf(char[] data)

Returns a String that is equivalent to the specified character array.

static String copyValueOf(char[] data, int offset, int count)

Returns a String that is equivalent to the specified character array.

boolean endsWith(String suffix)

Tests if this String ends with the specified suffix.

boolean equals(Object anObject)

Compares this String to the specified object.

boolean equalsIgnoreCase(String anotherString)

Compares this String to another String, ignoring case considerations.

byte[] getBytes()

Converts this String into bytes according to the platform's default character encoding, storing the result into a new byte array.

byte[] getBytes(String enc)

Converts this String into bytes according to the specified character encoding, storing the result into a new byte array.

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

Copies characters from this String into the destination character array.

int indexOf(int ch)

Returns the index within this String of the first occurrence of the specified character.

int indexOf(int ch, int fromIndex)

Returns the index within this String of the first occurrence of the specified character, starting the search at the specified index.

int indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring.

int indexOf(String str, int fromIndex)

Returns the index within this String of the first occurrence of the specified substring, starting at the specified index.

String intern()

Returns a canonical representation for the String object.

int lastIndexOf(int ch)

Returns the index within this String of the last occurrence of the specified character.

int lastIndexOf(int ch, int fromIndex)

Returns the index within this String of the last occurrence of the specified character, searching backward starting at the specified index.

int lastIndexOf(String str)

Returns the index within this String of the right-most occurrence of the specified substring.

int lastIndexOf(String str, int fromIndex)

Returns the index within this String of the last occurrence of the specified substring.

int length()

Returns the length of this String.

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

Tests if two string regions are equal.

boolean regionMatches(int toffset, String other, int ooffset, int len)

Tests if two String regions are equal.

String replace(char oldChar, char newChar)

Returns a new String resulting from replacing all occurrences of oldChar in this string with newChar.

boolean startsWith(String prefix)

Tests if this String starts with the specified prefix.

boolean startsWith(String prefix, int toffset)

Tests if this String starts with the specified prefix beginning a specified index.

String substring(int beginIndex)

Returns a new String that is a substring of this string.

String substring(int beginIndex, int endIndex)

Returns a new String that is a substring of this String.

char[] toCharArray()

Converts this String to a new character array.

String toLowerCase()

Converts all the characters in this String to lowercase using the rules of the default locale, which is returned by LocalegetDefault.

String toLowerCase(Locale locale)

Converts all the characters in this String to lowercase using the rules of the given Locale.

String toString()

This object (which is already a String!) is itself returned.

String toUpperCase()

Converts all the characters in this String to uppercase using the rules of the default locale, which is returned by LocalegetDefault.

String toUpperCase(Locale locale)

Converts all the characters in this String to uppercase using the rules of the given locale.

String trim()

Removes white space from both ends of this String.

static String valueOf(boolean b)

Returns the String representation of the boolean argument.

static String valueOf(char c)

Returns the String representation of the char argument.

static String valueOf(char[] data)

Returns the String representation of the char array argument.

static String valueOf(char[] data, int offset, int count)

Returns the string representation of a specific subarray of the char array argument.

static String valueOf(double d)

Returns the String representation of the double argument.

static String valueOf(float f)

Returns the String representation of the float argument.

static String valueOf(int i)

Returns the String representation of the int argument.

static String valueOf(long l)

Returns the String representation of the long argument.

static String valueOf(Object obj)

Returns the String representation of the Object argument.

Strings can be compared by both considering case and ignoring case; there are a pair of methods defined for comparing and testing the equality of two strings ignoring case and paying attention to case.

For example:

 String s1 = "Hello";  String s2 = "hello";  if( s1.equals( s2 ) ) {     System.out.println( "Strings are exactly equal   including case" );  }  else if( s1.equalsIgnoreCase( s2 ) ) {     System.out.println( "Strings are equal   ignoring case" );  }  else {     System.out.println( "Strings are not equal" );  } 

In this case, the output will be "Strings are equal ignoring case".

Strings have characters that are accessible via a zero-based index:

 String s1 = "Hello";  System.out.println( "Index 1 = " + s1.charAt( 1 ) ); // prints "e" 

This will be important for all string searches and substring extractions/modifications.

Other methods are provided to test the value of the beginning at the end of a string:

 String greeting = "Hello, World";  if( greeting.startsWith( "Hello" ) ) {    System.out.println( "String starts with Hello" );  }  if( greeting.endsWith( "World" ) ) {    System.out.println( "String ends with World" );  } 

The String class also provides a set of substring methods to extract parts of the string:

 String action = "Feed the baby";  System.out.println( action.substring( 5 ) ); // Prints "the baby"  System.out.println( action.substring( 5, 8 ) ); // Prints "the" 

An important facet of extracting a substring is to remember that the first value, or the beginning index, is an inclusive value, whereas the second value, or the end the index, is an exclusive value. So, the first value must be the zero-based index of the first letter of the substring that you want to extract, and the second value must be one plus the zero-based index of the last letter of the substring that you want to extract.

There are two methods to return a string with a modified case:

 String greeting = "Hello, World";  System.out.println( greeting ); // "Hello, World"  System.out.println( greeting.toLowerCase() ); // "hello, world"  System.out.println( greeting.toUpperCase() ); // "HELLO, WORLD" 

There also methods for locating substrings within string:

 String number = "one two one three one four";  // Prints "0"  System.out.println( "First index of 'one': " + number.indexOf( "one" ) );  // Prints "8"; look for "one" starting at index 1  System.out.println( "Next index of 'one': " + number.indexOf( "one", 1 ) );  // Prints "18"; look for "one" starting at end of the string and look backwards  System.out.println( "Next index of 'one': " + number.lastIndexOf( "one" ) ); 

Read through the JavaDoc and familiarize yourself with the other methods provided by the String class.

The StringBuffer Class

As I mentioned earlier, the String class is immutable and, therefore, you should not attempt to modify the value of a String. The StringBuffer class provides you a good interface to modify String values, and in the end convert the value back to a String class.

StringBuffer classes can be constructed initially empty, empty but with a specified starting size, or from an existing String (see Table 10.19).

Table 10.19. StringBuffer Method Summary

Method

Description

StringBuffer append(boolean b)

Appends the string representation of the boolean argument to the StringBuffer.

StringBuffer append(char c)

Appends the string representation of the char argument to this StringBuffer.

StringBuffer append(char[] str)

Appends the string representation of the char array argument to this StringBuffer.

StringBuffer append(char[] str, int offset, int len)

Appends the string representation of a subarray of the char array argument to this StringBuffer.

StringBuffer append(double d)

Appends the String representation of the double argument to this StringBuffer.

StringBuffer append(float f)

Appends the string representation of the float argument to this StringBuffer.

StringBuffer append(int i)

Appends the string representation of the int argument to this StringBuffer.

StringBuffer append(long l)

Appends the string representation of the long argument to this StringBuffer.

StringBuffer append(Object obj)

Appends the string representation of the Object argument to this StringBuffer.

StringBuffer append(String str)

Appends the String to this StringBuffer.

int capacity()

Returns the current capacity of the StringBuffer.

char charAt(int index)

The specified character of the sequence currently represented by the StringBuffer, as indicated by the index argument, is returned.

StringBuffer delete(int start, int end)

Removes the characters in a substring of this StringBuffer.

StringBuffer deleteCharAt(int index)

Removes the character at the specified position in this StringBuffer (shortening the StringBuffer by one character).

void ensureCapacity(int minimumCapacity)

Ensures that the capacity of the StringBuffer is at least equal to the specified minimum.

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

Characters are copied from this StringBuffer into the destination character array dst.

StringBuffer insert(int offset, boolean b)

Inserts the string representation of the Boolean argument into this StringBuffer.

StringBuffer insert(int offset, char c)

Inserts the String representation of the char argument into this StringBuffer.

StringBuffer insert(int offset, char[] str)

Inserts the String representation of the char array argument into this StringBuffer.

StringBuffer insert(int index, char[] str, int offset, int len)

Inserts the string representation of a subarray of the str array argument into this StringBuffer.

StringBuffer insert(int offset, double d)

Inserts the string representation of the double argument into this StringBuffer.

StringBuffer insert(int offset, float f)

Inserts the string representation of the float argument into this StringBuffer.

StringBuffer insert(int offset, int i)

Inserts the string representation of the second int argument into this StringBuffer.

StringBuffer insert(int offset, long l)

Inserts the string representation of the long argument into this StringBuffer.

StringBuffer insert(int offset, Object obj)

Inserts the string representation of the Object argument into this StringBuffer.

StringBuffer insert(int offset, String str)

Inserts the String into this StringBuffer.

int length()

Returns the length (character count) of this StringBuffer.

StringBuffer replace(int start, int end, String str)

Replaces the characters in a substring of this StringBuffer with characters in the specified String.

StringBuffer reverse()

The character sequence contained in this StringBuffer is replaced by the reverse of the sequence.

void setCharAt(int index, char ch)

The character at the specified index of this StringBuffer is set to ch.

void setLength(int newLength)

Sets the length of this StringBuffer.

String substring(int start)

Returns a new String that contains a subsequence of characters currently contained in this StringBuffer. The substring begins at the specified index and extends to the end of the StringBuffer.

String substring(int start, int end)

Returns a new String that contains a subsequence of characters currently contained in this StringBuffer.

String toString()

Converts to a String representing the data in this StringBuffer.

The StringBuffer class provides methods to append values to the end of the StringBuffer, insert values somewhere inside the StringBuffer, and delete values randomly within the StringBuffer. Now consider the previous example of reading seven characters from a text file:

 File:  abcdefg  Pseudo Code:  StringBuffer sb = new StringBuffer();  while( notEndOfFile ) {    sb.append( nextCharacter );  }  String value = sb.toString(); 

Comparing this to the earlier example, the variable value still has the value abcdefg, but the string table does not have any of the intermediate values.

Aside from having the capability to modify a String, the StringBuffer class provides you with the substring functionality as well as two new methods: reverse and replace. We are free to modify the value of a string because a string table doesn't limit us. Therefore, the reverse and replace methods are nice enhancements.

The StringTokenizer Class

The last category of string utility classes that I want to describe to you is the StringTokenizer class. The StringTokenizer class enables you to break a string into components called tokens. A prime example of using this would be reading a comma-separated value (CSV) file; it is fairly common to export data from the spreadsheets or a database into a flat-text file that has all the column values separated by commas. Consider the following string:

 1,2,3,4,5,6,7,8,9 

You might want to pull out all the values one by one, excluding the commas. The StringTokenizer class is constructed from a string that you want to tokenize and, optionally, a list of tokens and the Boolean stating whether to return each token. Table 10.20 lists the StringTokenizer constructors.

Table 10.20. StringTokenizer Constructors

Method

Description

StringTokenizer(String str)

Constructs a StringTokenizer for the specified string.

StringTokenizer(String str, String delim)

Constructs a StringTokenizer for the specified string.

StringTokenizer(String str, String delim, boolean returnDelims)

Constructs a StringTokenizer for the specified string.

Table 10.21 shows that methods of the StringTokenizer class.

Table 10.21. StringTokenizer Methods

Method

Description

int countTokens()

Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.

boolean hasMoreElements()

Returns the same value as the hasMoreTokens method.

boolean hasMoreTokens()

Tests if there are more tokens available from this tokenizer's string.

Object nextElement()

Returns the same value as the nextToken method, except that its declared return value is Object rather than String.

String nextToken()

Returns the next token from this StringTokenizer.

String nextToken(String delim)

Returns the next token in this StringTokenizer's string.

Here is an example using the StringTokenizer class to parse the aforementioned comma-separated value string:

 String source = "1,2,3,4,5,6,7,8,9";  StringTokenizer st = new StringTokenizer( source, ",", false );  while( st.hasMoreTokens() ) {    String value = st.nextToken();    System.out.println( value );  } 

The output of this code snippet is

 1  2  3  4  5  6  7  8  9 

       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net