1 import java.util.Vector;
4 // A stringsplitter object splits a string into a number of substrings,
5 // each separated by one separator character. Separator characters can be
6 // included in the string by escaping them with a \
7 public class StringSplitter
9 Vector parts = new Vector();
12 StringSplitter(String str, char sep)
17 StringSplitter(String str, char sep, boolean escape)
21 parts.addElement(current = new StringBuffer());
22 for(int i=0; i<str.length(); i++) {
23 char c = str.charAt(i);
24 if (c == '\\' && i != str.length()-1 && escape)
25 current.append(str.charAt(++i));
27 parts.addElement(current = new StringBuffer());
34 // The number of tokens left in the string
37 return parts.size() - pos;
41 // Can we call nextToken?
42 boolean hasMoreTokens()
44 return pos < parts.size();
48 // Returns the string value of the next token
51 if (pos < parts.size())
52 return ((StringBuffer)parts.elementAt(pos++)).toString();
58 // Returns a vector of strings split from the given input string
67 // The complement of StringSplitter. Takes a number of substrings and adds
68 // them to a string, separated by some character. If the separator character
69 // appears in one of the substrings, escape it with a \
73 StringBuffer str = new StringBuffer();
76 // Create a new StringJoiner using the given separator
83 // Add one string, and a separator
88 for(int i=0; i<s.length(); i++) {
90 if (c == sep || c == '\\') str.append('\\');
97 // Get the resulting string
98 public String toString()
100 return str.toString();