自己写的stringToken

//A.java

public class A {

    
// 找出字符串中的最长单词
    public static void main(String[] args) {
        String str 
= "all mimsy were the borogoves.";
        
// System.out.println(exec(str));
        stringToken token = new stringToken(str);
        
while (token.hasMoreTokens()) {
            System.out.println(token.nextToken());
        }
    }

    
// public static String exec(String str) {
    
// // StringTokenizer token=new StringTokenizer(str," .",false);
    
// StringTokenizer token = new StringTokenizer(str, " .");
    
// String result = "";
    
// int a = 0;
    
// while (token.hasMoreTokens()) {
    
// if ((result = token.nextElement().toString()).length() > a) {
    
// a = result.length();
    
// }
    
// }
    
// return result;
    
// }
}

class stringToken {
    String str;
    String delim;
    
boolean returnDelims;
    
int count;
    String temp;

    
public stringToken(String str) {
        temp 
= this.str = str;
    }

    
public stringToken(String str, String delim) {
        temp 
= this.str = str;
        
this.delim = delim;
    }

    
public stringToken(String str, String delim, boolean returnDelims) {
        temp 
= this.str = str;
        
this.delim = delim;
        
this.returnDelims = returnDelims;
    }

    
public boolean hasMoreTokens() {
        
return (temp != null);
    }

    
public String nextToken() {
        String nextToken 
= null;
        
// 最简单的构造函数
        if (delim == null && temp != null) {
            
if (temp.length() > 0) {
                
if (temp.indexOf(" "!= -1) {
                    nextToken 
= temp.substring(0, temp.indexOf(" "));
                    temp 
= temp.substring(temp.indexOf(" "+ 1);
                } 
else {
                    nextToken 
= temp;
                    temp 
= null;
                }
            }
        }
        
return nextToken;
    }
}

posted on 2009-11-19 09:08  我为Java而努力  阅读(875)  评论(0)    收藏  举报

导航