Getting Information About a String Object

   

Getting Information About a String Object

After you have your String object constructed , you can call upon the methods in the String class to obtain information about the string. To get the length of the string, for example, you can call the length method, like this:

 String str = "This is a string"; int len = str.length(); 

The value of the len variable should be 16, which is the length of the string (including spaces, of course).

If you want to know whether a string starts with a certain prefix, you can call the startsWith method, like this:

 String str = "This is a string"; boolean result = str.startsWith("This"); 

Here, the boolean variable result is equal to true, because str does indeed start with This. In the following example, result is false:

 String str = "This is a string"; boolean result = str.startsWith("is"); 

A similar method is endsWith , which determines whether the String object ends with a given set of characters . You use that method as follows :

 String str = "This is a string"; boolean result = str.endsWith("string"); 

In this example, result ends up equal to true, whereas the following code segment sets result equal to false:

 String str = "This is a string"; boolean result = str.endsWith("This"); 

If you're setting up a table for strings that you want to be able to locate quickly, you can use a hash table. To get a hash code for a string, you can call the hashCode method:

 String str = "This is a string"; int hashcode = str.hashCode(); 

If you want to find the location of the first occurrence of a character within a string, use the indexOf method:

 String str = "This is a string"; int index = str.indexOf('a'); 

In this example, index is equal to 8, which is the index of the first a in the string.

Note

Remember that the first position in a String has an index of 0 and the last position in a String has an index of length “1.


To find the location of subsequent characters, you can use two versions of the indexOf method. To find the first occurrence of i, for example, you might use these lines:

 String str = "This is a string"; int index = str.indexOf('i'); 

This gives index a value of 2. To find the next occurrence of i, you can use a line similar to this:

 index = str.indexOf('i', index+1); 

By including the index+1 as the method's second argument, you're telling Java to start searching at index 3 in the string (the old value of index, plus 1). This results in index being equal to 5, which is the location of the second occurrence of i in the string. If you called the previous line again, index would be equal to 13, which is the location of the third i in the string.

You can also search for characters backward through a string, using the lastIndexOf method:

 String str = "This is a string"; int index = str.lastIndexOf("i"); 

Here, index is equal to 13. To search backward for the next i, you might use a line like this:

 index = str.lastIndexOf('i', index-1); 

Now, index is equal to 5, because the index “1 as the second argument tells Java where to begin the backward search. The variable index was equal to 13 after the first call to lastIndexOf(), so in the second call, index “1 equals 12.

There are also versions of indexOf and lastIndexOf that search for substrings within a string. The following example sets index to 10, for instance:

 String str = "This is a string"; int index = str.indexOf("string"); 

Listing 8.1 is a small application that gives you a chance to experiment with the indexOf method. When you run the application, you provide two strings on the command line. The first string is the string that you want to search through and the second is the substring that you are looking for.

Listing 8.1 IndexOfExample.java ” An Application that Searches for Substrings
 public class IndexOfExample {   // Default Constructor   public IndexOfExample()   {     super();   }   // Method that actually performs the search   public int doSearch( String str, String subStr )   {     return str.indexOf( subStr );   }   // Main Method   public static void main( String[] args )   {     if ( args.length != 2 )     {       System.out.println( "Usage: java IndexOfExample <String> <Substring>" );       System.exit( 0 );     }     // Create an instance of the example     IndexOfExample example = new IndexOfExample();     // Get the strings passed in from the command line     String str = args[0];     String subStr = args[1];     // Inform the user what is going on     System.out.println( "Searching string: " + str + " for: " + subStr );     // perform the search     int index = example.doSearch( str, subStr );     // check the result     if ( index != -1 )     {       System.out.println( "Found the substring at position " + index );     }     else     {       System.out.println( "Did not find the substring" );     }   } } 

Here is what the output looks like when you run the application in Listing 8.1:

 C:\jdk1.3se_book\classes>java IndexOfExample "An apple a day" day Searching string: An apple a day for: day Found the substring at position 11 C:\jdk1.3se_book\classes> 

Notice that quotes had to be put around the command line argument "An apple a day" in the search query so that the entire quote would be considered as a single argument.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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