KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > util > FullStringTokenizer


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.util;
26
27 public class FullStringTokenizer {
28
29     private String JavaDoc string;
30     private String JavaDoc delimiter;
31
32     private int index = 0;
33     private int length = 0;
34     private int delimiterLength;
35
36     public FullStringTokenizer(String JavaDoc string, String JavaDoc delimiter) {
37         this.string = string;
38         this.delimiter = delimiter;
39         delimiterLength = delimiter.length();
40         length = string.length();
41     }
42
43    /**
44     * Determine if there are more tokens available
45     */

46     public boolean hasMoreTokens() {
47         return (index < length);
48     }
49
50    /**
51     * Get the next token
52     */

53     public String JavaDoc nextToken() {
54         String JavaDoc s = this.string;
55         int nextToken = s.indexOf(delimiter, index);
56         //Done finding tokens
57
if (nextToken < 0) {
58             // fixed to return whatever is left in the string (for the case
59
// of "[tok]data[tok]data" <- no ending token)
60
String JavaDoc tok = s.substring(index,length);
61             index = length;
62             return tok;
63         }
64         else if (nextToken == index) {
65             //We found an empty token
66
index += delimiterLength;
67             return "";
68         }
69         else {
70             String JavaDoc result = s.substring(index, nextToken);
71             index = nextToken + delimiterLength;
72             return result;
73         }
74     }
75
76     /**
77      * Test program.
78      */

79     public static void main(String JavaDoc [] args) {
80         String JavaDoc testString = "||this is|| a test||||of the system||||";
81         FullStringTokenizer tokens = new FullStringTokenizer(testString, "||");
82         while(tokens.hasMoreTokens()) {
83             System.out.println("> " + tokens.nextToken());
84         }
85     }
86 }
87
Popular Tags