if (s.length() == 0) {
return "";
}
// Flags to determine if we are in quotes
boolean inSingle = false;
boolean inDouble = false;
boolean found = false;
// Current offset in string
int offset = 0;
char c;
// Special characters. If the string starts with any of these
// characters, it will be considered a word. Otherwise, if any
// of the characters are found outside quoted strings, it will
// signify the end of a word
String special = "()*=,? <>";
// Skip any leading blanks
while (s.charAt(offset) == ' ") {
offset++;
// No more string left.
if (offset > s.length()) {
return "";
}
}
int si;
// Look for special characters
c = s.charAt(offset);
for (si = 0; si < special.length(); si++) {
if (c == special.charAt(si)) {
return s.substring(0, offset + 1);
}
}
// Loop while more characters exist in the string
while (offset < s.length()) {
c = s.charAt(offset);
// Look for single or double quotes
if (c == '\'") {
if (!inDouble) {
if (inSingle) {
offset++;
break;
}
inSingle = true;
}
}
if (c == '"") {
if (!inSingle) {
if (inDouble) {
offset++;
break;
}
inDouble = true;
}
}
// If not inside a double or single quote, examine the next
// character
if ((!inDouble) && (!inSingle)) {
// If a special character exists, it terminates this word
for (si = 0; si < special.length(); si++) {
if (c == special.charAt(si)) {
found = true;
break;
}
}
if (found) {
break;
}
}
offset++;
}
return s.substring(0, offset);