1 57 58 package com.Yasna.util; 59 60 public class FullStringTokenizer { 61 62 private String string; 63 private String delimiter; 64 65 private int index = 0; 66 private int length = 0; 67 private int delimiterLength; 68 69 public FullStringTokenizer(String string, String delimiter) { 70 this.string = string; 71 this.delimiter = delimiter; 72 delimiterLength = delimiter.length(); 73 length = string.length(); 74 } 75 76 79 public boolean hasMoreTokens() { 80 return (index < length); 81 } 82 83 86 public String nextToken() { 87 String s = this.string; 88 int nextToken = s.indexOf(delimiter, index); 89 if (nextToken < 0) { 91 String tok = s.substring(index,length); 94 index = length; 95 return tok; 96 } 97 else if (nextToken == index) { 98 index += delimiterLength; 100 return ""; 101 } 102 else { 103 String result = s.substring(index, nextToken); 104 index = nextToken + delimiterLength; 105 return result; 106 } 107 } 108 109 112 public static void main(String [] args) { 113 String testString = "||this is|| a test||||of the system||||"; 114 FullStringTokenizer tokens = new FullStringTokenizer(testString, "||"); 115 while(tokens.hasMoreTokens()) { 116 System.out.println("> " + tokens.nextToken()); 117 } 118 } 119 } 120 | Popular Tags |