Jtest logo




Contents  Previous  Next  Index

OPT.UST


Use StringTokenizer instead of 'indexOf()' or 'substring()'

Description

This rule flags code that uses `indexOf()' or `substring()' instead of StringTokenizer.

String parsing is commonly performed in many applications. Using 'indexOf()' and 'substring()' to parse a String can cause a StringIndexOutOfBoundsException. The StringTokenizer class makes String parsing bit easier, and is still quite efficient.

Example

 package OPT;
 
 public class UST {
     void parseString(String string) {
         int index = 0;
         while ((index = string.indexOf(".", index)) != -1) {
             System.out.println (string.substring(index, 
string.length()));
         }
     }
 }

Reference

Larman, G, Guthrie, R Java 2 Performance and Idiom Guide. Prentice Hall, 1999, 248 - 249.


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft